The org.json library is easy to use.
Just remember (while casting or using methods like getJSONObject and getJSONArray) that in JSON notation
[ … ]represents an array, so library will parse it toJSONArray{ … }represents an object, so library will parse it toJSONObject
Example code below:
import org.json.*;
String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");
JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
String post_id = arr.getJSONObject(i).getString("post_id");
......
}
You may find more examples from: Parse JSON in Java
Downloadable jar: http://mvnrepository.com/artifact/org.json/json
Answer from user1931858 on Stack OverflowThe org.json library is easy to use.
Just remember (while casting or using methods like getJSONObject and getJSONArray) that in JSON notation
[ … ]represents an array, so library will parse it toJSONArray{ … }represents an object, so library will parse it toJSONObject
Example code below:
import org.json.*;
String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");
JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
String post_id = arr.getJSONObject(i).getString("post_id");
......
}
You may find more examples from: Parse JSON in Java
Downloadable jar: http://mvnrepository.com/artifact/org.json/json
For the sake of the example lets assume you have a class Person with just a name.
private class Person {
public String name;
public Person(String name) {
this.name = name;
}
}
Jackson (Maven)
My personal favourite and probably the most widely used.
ObjectMapper mapper = new ObjectMapper();
// De-serialize to an object
Person user = mapper.readValue("{\"name\": \"John\"}", Person.class);
System.out.println(user.name); //John
// Read a single attribute
JsonNode nameNode = mapper.readTree("{\"name\": \"John\"}");
System.out.println(nameNode.get("name").asText());
Google GSON (Maven)
Gson g = new Gson();
// De-serialize to an object
Person person = g.fromJson("{\"name\": \"John\"}", Person.class);
System.out.println(person.name); //John
// Read a single attribute
JsonObject jsonObject = new JsonParser().parseString("{\"name\": \"John\"}").getAsJsonObject();
System.out.println(jsonObject.get("name").getAsString()); //John
Org.JSON (Maven)
This suggestion is listed here simply because it appears to be quite popular due to stackoverflow reference to it. I would not recommend using it as it is more a proof-of-concept project than an actual library.
JSONObject obj = new JSONObject("{\"name\": \"John\"}");
System.out.println(obj.getString("name")); //John
Best Java JSON Parser: Gson or Jackson?
Jackson, but mostly because I tend to use it from Spring MVC and it just works without any extra effort.
More on reddit.comHow can I parse JSON in Java? - LambdaTest Community
How to parse Json with Java.
Google's GSON is great. Create the class structure, deserialize from json into java objects. Couldn't be simpler!
More on reddit.comParsing JSON object using Java
Videos
I need to parse a JSON file and I don't know how to do so in VSC, I've looked at youtube tutorials, ive looked online and they've all lead me to nothing.
IF im not explaning this right then this is what I'm trying to accomplish (This is in JavaScript)
JSON.parse(fs.readFileSync(INPUT));
Which library do you prefer when working with JSON, Gson or Jackson? Seems like the Java community is split in half. Which do you prefer and why?