📘 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.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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 JSON Read Example
package net.javaguides.jsonp.tutorial;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
/**
* Class to read json from a posts.json file.
* @author Ramesh fadatare
*
*/
public class ReadJSON {
public static void main(String[] args) throws IOException {
InputStream fis = new FileInputStream("posts.json");
// create JsonReader object
JsonReader jsonReader = Json.createReader(fis);
// get JsonObject from JsonReader
JsonObject jsonObject = jsonReader.readObject();
// we can close IO resource and JsonReader now
jsonReader.close();
fis.close();
// Retrieve data from JsonObject and create Post bean
Post post = new Post();
post.setId(jsonObject.getInt("id"));
post.setTitle(jsonObject.getString("title"));
post.setDescription(jsonObject.getString("description"));
post.setContent(jsonObject.getString("content"));
// reading arrays from json
JsonArray jsonArray = jsonObject.getJsonArray("tags");
String[] tags = new String[jsonArray.size()];
int index = 0;
for (JsonValue value: jsonArray) {
tags[index++] = value.toString();
}
post.setTags(tags);
// print post object
System.out.println(post.toString());
}
}
Post [id=100, title=JSONP Tutorial, description=Post about JSONP,
content=HTML content here, tags=["Java", "JSON"]]
Java JSON Write 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.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;
/**
* Class to write json to a posts.json file.
* @author Ramesh fadatare
*
*/
public class WriteJSON {
public static void main(String[] args) throws FileNotFoundException {
Post post = createPost();
JsonObjectBuilder postBuilder = Json.createObjectBuilder();
JsonArrayBuilder tagsBuilder = Json.createArrayBuilder();
for (String tag: post.getTags()) {
tagsBuilder.add(tag);
}
postBuilder.add("id", post.getId())
.add("title", post.getTitle())
.add("description", post.getDescription())
.add("content", post.getContent())
.add("tags", tagsBuilder);
JsonObject postJsonObject = postBuilder.build();
System.out.println("Post JSON String -> " + postJsonObject);
//write to file
OutputStream os = new FileOutputStream("posts.json");
JsonWriter jsonWriter = Json.createWriter(os);
Map < String, Boolean > config = new HashMap < String, Boolean > ();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory factory = Json.createWriterFactory(config);
jsonWriter = factory.createWriter(os);
jsonWriter.writeObject(postJsonObject);
jsonWriter.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;
}
}
JsonObjectBuilder postBuilder = Json.createObjectBuilder();
JsonArrayBuilder tagsBuilder = Json.createArrayBuilder();
postBuilder.add("id", post.getId())
.add("title", post.getTitle())
.add("description", post.getDescription())
.add("content", post.getContent())
.add("tags", tagsBuilder);
JsonObject postJsonObject = postBuilder.build();
//write to file
OutputStream os = new FileOutputStream("posts.json");
JsonWriter jsonWriter = Json.createWriter(os);
Map<String, Boolean> config = new HashMap<String, Boolean>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory factory = Json.createWriterFactory(config);
jsonWriter = factory.createWriter(os);
jsonWriter.writeObject(postJsonObject);
jsonWriter.close();
Comments
Post a Comment
Leave Comment