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 to JSONArray
  • { … } represents an object, so library will parse it to JSONObject

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 Overflow
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 2.8 › com › fasterxml › jackson › core › JsonParser.html
JsonParser (Jackson-core 2.8.0 API)
If current token is of type ... returns one of JsonParser.NumberType constants; otherwise returns null. ... Numeric accessor that can be called when the current token is of type JsonToken.VALUE_NUMBER_INT and it can be expressed as a value of Java byte primitive ...
Top answer
1 of 16
962

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 to JSONArray
  • { … } represents an object, so library will parse it to JSONObject

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

2 of 16
714

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
Discussions

Week 109 — How can one parse JSON text in a Java application?
For that we'll use JsonParser: ... Applications should use an existing library for working with JSON. The most commonly used one is Jackson, but other popular libraries include Google's GSON and Jakarta EE's JSON-P. GitHub - google/gson: A Java serialization/deserialization library ... A Java serialization/deserialization library to convert Java Objects into JSON and back - google/gson ... A quick Jackson example... More on answeroverflow.com
🌐 answeroverflow.com
January 19, 2025
3 ways to parse JSON String to Object in Java [Jackson, Gson, and json-simple Example]
Turns out i read your whole article on that link. Thanks. The least i use was gson(). Maybe i will try it more soon More on reddit.com
🌐 r/JavaProgramming
3
3
June 10, 2024
How can I parse JSON in Java? - LambdaTest Community
How can I parse JSON in Java? I have the following JSON text. How can I parse it to get the values of pageName, pagePic, post_id, etc.? { "pageInfo": { "pageName": "abc", "pagePic": "http://example.com/content.jpg" }, "posts": [ { "post_id": "123456789012_123456789012", "actor_id": "1234567890", ... More on community.lambdatest.com
🌐 community.lambdatest.com
1
March 31, 2024
using JSONArray, JSONObject etc. to parse JSON with Java
I'd highly advise you to use the ObjectMapper instead of JSONArray and JSONObject. Try to mimic the json objects structure with a class, create 0 field and all argument contrstructors, getters and setters and simply use new ObjectMapper().readValue(yourJsonString, YouMappedObject.class) More on reddit.com
🌐 r/javahelp
6
5
December 6, 2021
🌐
Mkyong
mkyong.com › home › java › how to parse json string with jackson
How to parse JSON string with Jackson - Mkyong.com
April 23, 2024 - package com.mkyong.json.jackson; import com.fasterxml.jackson.databind.ObjectMapper; import com.mkyong.json.model.Staff; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class ConvertJavaObjectToJsonExample { private static final ObjectMapper MAPPER = new ObjectMapper(); public static void main(String[] args) throws IOException { Staff staff = createStaff(); // Java object to JSON file - default compact-print // MAPPER.writeValue(new File("output.json"), staff); // write Java object to
🌐
Medium
medium.com › @AlexanderObregon › javas-jsonparser-parse-method-explained-76b126f6110e
Java’s JsonParser.parse() Method Explained | Medium
October 13, 2024 - Learn how to use Java's JsonParser.parse() method for parsing JSON data, including examples, performance tips, and comparisons to modern alternatives.
🌐
Blogger
javarevisited.blogspot.com › 2022 › 03 › 3-examples-to-parse-json-in-java-using.html
3 ways to parse JSON String to Object in Java [Jackson, Gson, and json-simple Example]
April 22, 2023 - Here is an example of parsing a JSON message into a Java object. We have a class called JSONParser which is similar to ObjectMapper of Jackson but here we are parsing into JSONObject rather than a POJO, hence we need to extract each attribute ...
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › stream › JsonParser.html
JsonParser (Java(TM) EE 7 Specification APIs)
The class Json contains methods to create parsers from input sources (InputStream and Reader). The following example demonstrates how to create a parser from a string that contains an empty JSON array: JsonParser parser = Json.createParser(new StringReader("[]")); The class JsonParserFactory ...
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › jackson-json-java-parser-api-example-tutorial
Jackson JSON Java Parser API Example Tutorial | DigitalOcean
August 3, 2022 - package com.journaldev.jackson.json; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.journaldev.jackson.model.Address; import com.journaldev.jackson.model.Employee; public class JacksonStreamingReadExample { public static void main(String[] args) throws JsonParseException, IOException { //create JsonParser object Json
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-jsonparser.html
Jackson JsonParser
June 6, 2015 - Here is a JsonParser example that simply loops through all the tokens and print them out to System.out.
🌐
GeeksforGeeks
geeksforgeeks.org › java › parse-json-java
How to parse JSON in Java - GeeksforGeeks
December 23, 2025 - This example reads and parses the previously created JSON file. ... import java.io.FileReader; import java.util.Iterator; import java.util.Map; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.pars...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › json
Java JSON parser Example - Java Code Geeks
September 24, 2021 - After we create an instance of JSONParser, we create a JSONObject by parsing the FileReader of our .json file. This JSONObject contains a collection of key-value pairs, from which we can get every value of the JSON file.
🌐
Baeldung
baeldung.com › home › json › using static methods instead of deprecated jsonparser
Using Static Methods Instead of Deprecated JsonParser | Baeldung
June 20, 2024 - We can parse a JSON string directly into JsonObject without using a deprecated instance of JsonParser using the parseString() static method.
🌐
Javadoc.io
javadoc.io › doc › com.fasterxml.jackson.core › jackson-core › latest › com › fasterxml › jackson › core › JsonParser.html
JsonParser - jackson-core 2.21.1 javadoc
Bookmarks · Latest version of com.fasterxml.jackson.core:jackson-core · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-core · Current version 2.21.1 · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-core/2.21.1 · package-list path (used for javadoc generation -link ...
🌐
Vertica
docs.vertica.com › 11.1.x › en › extending › developing-udxs › user-defined-load-udl › user-defined-parser › java-example-json-parser
Java example: JSON parser | Vertica 11.1.x
July 14, 2023 - => CREATE LIBRARY json -> AS '/opt/vertica/packages/flextable/examples/java/output/json.jar' -> DEPENDS '/opt/vertica/bin/gson-2.2.4.jar' language 'java'; CREATE LIBRARY => CREATE PARSER JsonParser AS LANGUAGE 'java' -> NAME 'com.vertica.flex.JsonParserFactory' LIBRARY json; CREATE PARSER FUNCTION
🌐
Answer Overflow
answeroverflow.com › m › 1330582551223468164
Week 109 — How can one parse JSON text in a Java application? - Java Community | Help. Code. Learn.
January 19, 2025 - Still, parsing JSON in Java is easy, as there are many parser libraries available! This leads us to the first question: "which library should I use?" Currently, these are the two most popular JSON parsers: - Jackson - GSON Which one you use is only up to you. After choosing a library, the next step is to include it in our project. In this example I will use the GSON libray, as in my
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-implement-a-json-parser
Java Program to Implement a JSON Parser - GeeksforGeeks
September 30, 2021 - // Java Program to Implement JSON Parser // Importing required classes import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; // Defining constants for json parsers enum CONSTANTS { CURLY_OPEN_BRACKETS('{'), CURLY_CLOSE_BRACKETS('}'), SQUARE_OPEN_BRACKETS('['), SQUARE_CLOSE_BRACKETS(']'), COLON(':'), COMMA(','), SPECIAL('|'); private final char constant; // Constructor CONSTANTS(char constant) { this.constant = constant; } // Method // Overriding exiting toString() method @Override public String toString() { return String.valueOf(constant); } } // Class 1 // To par
🌐
Jenkov
jenkov.com › tutorials › java-json › gson-jsonparser.html
GSON - JsonParser
January 27, 2021 - Here is an example: JsonObject jsonObject = jsonTree.getAsJsonObject(); JsonElement f1 = jsonObject.get("f1"); JsonElement f2 = jsonObject.get("f1"); You can inspect the type of each of these fields too, just like with the first JsonElement ...
🌐
Baeldung
baeldung.com › home › json › jackson › json in java
JSON in Java | Baeldung
May 11, 2024 - We’ll discuss the most common JSON processing libraries in Java: ... We’re following a simple structure for each library – first some useful resources to get started (both here on Baeldung as well as external). Then we’re going to go over a basic code example, just to see how working with the library actually looks like.
🌐
Reddit
reddit.com › r/javaprogramming › 3 ways to parse json string to object in java [jackson, gson, and json-simple example]
r/JavaProgramming on Reddit: 3 ways to parse JSON String to Object in Java [Jackson, Gson, and json-simple Example]
June 10, 2024 - News, Technical discussions, research papers and assorted things of interest related to the Java programming language NO programming help, NO learning Java related questions, NO installing or downloading Java questions, NO JVM languages - Exclusively Java
🌐
LambdaTest Community
community.lambdatest.com › general discussions
How can I parse JSON in Java? - LambdaTest Community
March 31, 2024 - How can I parse JSON in Java? I have the following JSON text. How can I parse it to get the values of pageName, pagePic, post_id, etc.? { "pageInfo": { "pageName": "abc", "pagePic": "http://example.com/content.jpg" }, "posts": [ { "post_id": "123456789012_123456789012", "actor_id": "1234567890", "picOfPersonWhoPosted": "http://example.com/photo.jpg", "nameOfPersonWhoPosted": "Jane Doe", "message": "Sounds cool.