📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Check out complete JSON-P tutorial at Java JSON Processing Tutorial.
- Check out complete JSON-P tutorial at Java JSON Processing Tutorial.
- Check out the Jackson JSON tutorial at https://www.javaguides.net/p/java-jackson-json-tutorial-with-examples.html.
- Check out google GSON tutorial at Google GSON Tutorial.
JSON-P
Add Dependencies
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
{
"id": 100,
"title": "JSONP Tutorial",
"description": "Post about JSONP",
"content": "HTML content here",
"tags": [
"Java",
"JSON"
]
}
package net.javaguides.jsonp.tutorial;
import java.util.Arrays;
public class Post {
private int id;
private String title;
private String description;
private String content;
private String[] tags;
public Post() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String[] getTags() {
return tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
@Override
public String toString() {
return "Post [id=" + id + ", title=" + title + ", description=" + description + ", content=" + content +
", tags=" + Arrays.toString(tags) + "]";
}
}
Java Create JSON Example
package net.javaguides.jsonp.tutorial;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonGeneratorFactory;
/**
* Class to create a json using jsonp library.
* @author Ramesh fadatare
*
*/
public class CreateJSON {
public static void main(String[] args) throws FileNotFoundException {
OutputStream fos = new FileOutputStream("posts.json");
Map < String, Boolean > config = new HashMap < String, Boolean > ();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonGeneratorFactory factory = Json.createGeneratorFactory(config);
JsonGenerator jsonGenerator = factory.createGenerator(fos);
Post post = createPost();
jsonGenerator.writeStartObject(); // {
jsonGenerator.write("id", post.getId()); // "id":123
jsonGenerator.write("title", post.getTitle());
jsonGenerator.write("description", post.getDescription());
jsonGenerator.write("content", post.getContent());
jsonGenerator.writeStartArray("tags");
for (String tag: post.getTags()) {
jsonGenerator.write(tag);
}
jsonGenerator.writeEnd(); // end of phone num array
jsonGenerator.writeEnd(); // }
jsonGenerator.close();
}
private static Post createPost() {
// create a post
Post post = new Post();
post.setTitle("JSONP Tutorial");
post.setId(100);
post.setDescription("Post about JSONP");
post.setContent("HTML content here");
String[] tags = {
"Java",
"JSON"
};
// create some predefined tags
post.setTags(tags);
// set tags to post
return post;
}
}
OutputStream fos = new FileOutputStream("posts.json");
Map<String, Boolean> config = new HashMap<String, Boolean>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonGeneratorFactory factory = Json.createGeneratorFactory(config);
JsonGenerator jsonGenerator = factory.createGenerator(fos);
``
An object is started with writeStartObject(). It is later ended with writeEnd():
```java
jsonGenerator.writeStartObject();
jsonGenerator.writeStartArray("tags");
Post post = createPost();
jsonGenerator.writeStartObject(); // {
jsonGenerator.write("id", post.getId()); // "id":123
jsonGenerator.write("title", post.getTitle());
jsonGenerator.write("description", post.getDescription());
jsonGenerator.write("content", post.getContent());
private static Post createPost() {
// create a post
Post post = new Post();
post.setTitle("JSONP Tutorial");
post.setId(100);
post.setDescription("Post about JSONP");
post.setContent("HTML content here");
String[] tags = {"Java", "JSON"};
// create some predefined tags
post.setTags(tags);
// set tags to post
return post;
}
- Check out complete JSON-P tutorial at Java JSON Processing Tutorial.
- Check out the Jackson JSON tutorial at https://www.javaguides.net/p/java-jackson-json-tutorial-with-examples.html.
- Check out google GSON tutorial at Google GSON Tutorial.
Comments
Post a Comment
Leave Comment