🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this example, we will see a quick example to read a JSON file with JSON-P library.
The code example is available at my Github repository.
- 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
JSON Processing (JSON-P) is a Java API to process (for e.g. parse, generate, transform and query) JSON messages. It produces and consumes JSON text in a streaming fashion (similar to StAX API for XML) and allows to build a Java object model for JSON text using API classes (similar to DOM API for XML).
Read more about JSON-P at official documentation - https://javaee.github.io/jsonp.
Add Dependencies
JSON-P is the reference implementation for Java JSON Processing API. We can use this in maven project by adding the following 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>
Let's create a JSON file named "posts.json":
{
"id": 100,
"title": "JSONP Tutorial",
"description": "Post about JSONP",
"content": "HTML content here",
"tags": [
"Java",
"JSON"
]
}
We have Java bean classes that represent above JSON format as:
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
In this example, we are reading above "posts.json" file. We use JsonReader and JsonObject interfaces to read and extract fields and display.
Here is complete source code:
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());
}
}
Output:
Post [id=100, title=JSONP Tutorial, description=Post about JSONP,
content=HTML content here, tags=["Java", "JSON"]]
The code example is available at my Github repository.
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment