In this post, we will learn how to parse a JSON using 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 complete JSON-P tutorial at Java JSON Processing 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>
Java Parse JSON Example
JsonParser parses JSON using the pull parsing programming model. In this model, the client code controls the thread and calls the method next() to advance the parser to the next state after processing each element.
The parser generates one of the following events: START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY, KEY_NAME, VALUE_STRING, VALUE_NUMBER, VALUE_TRUE, VALUE_FALSE, and VALUE_NULL.
Let's create a simple json inside a file named "posts.json".
[
{
"id": 100,
"title": "JSONP Tutorial",
"description": "Post about JSONP",
"content": "HTML content here",
"tags": [
"Java",
"JSON"
]
},
{
"id": 101,
"title": "GSON Tutorial",
"description": "Post about GSON",
"content": "HTML content here",
"tags": [
"Java",
"JSON"
]
},
{
"id": 102,
"title": "Simple JSON Tutorial",
"description": "Post about Simple JSON",
"content": "HTML content here",
"tags": [
"Java",
"JSON"
]
}
]
In the example, we parse the above posts.json file using the JSON-P streaming API.
package net.javaguides.jsonp.tutorial;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;
import javax.json.stream.JsonParserFactory;
/**
* Class to parse a json using jsonp.
* @author Ramesh fadatare
*
*/
public class ParseJSON {
public static void main(String[] args) throws FileNotFoundException {
InputStream is = new FileInputStream("posts.json");
JsonParserFactory factory = Json.createParserFactory(null);
JsonParser parser = factory.createParser(is, StandardCharsets.UTF_8);
if (!parser.hasNext() && parser.next() != JsonParser.Event.START_ARRAY) {
return;
}
// looping over object attributes
while (parser.hasNext()) {
Event event = parser.next();
// starting object
if (event == JsonParser.Event.START_OBJECT) {
while (parser.hasNext()) {
event = parser.next();
if (event == JsonParser.Event.KEY_NAME) {
String key = parser.getString();
switch (key) {
case "id":
parser.next();
System.out.printf("id: %s%n", parser.getString());
break;
case "title":
parser.next();
System.out.printf("title: %s%n", parser.getString());
break;
case "description":
parser.next();
System.out.printf("description: %s%n%n", parser.getString());
break;
case "content":
parser.next();
System.out.printf("content: %s%n%n", parser.getString());
break;
}
}
}
}
}
}
}
Output:
id: 100
title: JSONP Tutorial
description: Post about JSONP
content: HTML content here
id: 101
title: GSON Tutorial
description: Post about GSON
content: HTML content here
id: 102
title: Simple JSON Tutorial
description: Post about Simple JSON
content: HTML content here
A JsonParser is created from JsonParserFactory:
InputStream is = new FileInputStream("posts.json");
JsonParserFactory factory = Json.createParserFactory(null);
JsonParser parser = factory.createParser(is, StandardCharsets.UTF_8);
First, we pass the beginning of the array:
if (!parser.hasNext() && parser.next() != JsonParser.Event.START_ARRAY) {
return;
}
Then we go through the array in a while loop. The parser hasNext() method returns false when we reach the end of the array. We pull the next parsing event with next():
// looping over object attributes
while (parser.hasNext()) {
Event event = parser.next();
// starting object
if (event == JsonParser.Event.START_OBJECT) {
...
In another while loop, we go through the keys of the current object:
while (parser.hasNext()) {
event = parser.next();
if (event == JsonParser.Event.KEY_NAME) {
...
In the switch statement we check for the key name and get its value with getString():
String key = parser.getString();
switch (key) {
case "name":
parser.next();
System.out.printf("Name: %s%n", parser.getString());
break;
...
The code example is available at my Github repository.
Check out complete JSON-P tutorial at Java JSON Processing Tutorial.
Check out complete JSON-P tutorial at Java JSON Processing Tutorial.
Comments
Post a Comment
Leave Comment