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
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
🌐
Medium
medium.com › @AlexanderObregon › javas-jsonparser-parse-method-explained-76b126f6110e
Java’s JsonParser.parse() Method Explained | Medium
October 13, 2024 - Specifically, the JsonParser interface provides low-level access to the JSON stream, while the JsonReader class provides a higher-level API for reading JSON data as objects and arrays.
🌐
DigitalOcean
digitalocean.com › community › tutorials › jackson-json-java-parser-api-example-tutorial
Jackson JSON Java: Parser API Examples & Tutorial | DigitalOcean
August 3, 2022 - Parse JSON in Java using Jackson library. Complete tutorial with examples for serialization, deserialization, annotations, and advanced JSON processing.
🌐
GeeksforGeeks
geeksforgeeks.org › java › parse-json-java
How to parse JSON in Java - GeeksforGeeks
December 23, 2025 - 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.parser.JSONParser; public class JSONReadExample { public static void main(String[] args) throws Exception { Object obj = new JSONParser().parse(new FileReader("JSONExample.json")); JSONObject jo = (JSONObject) obj; String firstName = (String) jo.get("firstName"); String lastName = (String) jo.get("lastName"); long age = (long) jo.get("age"); System.out.println(firstName); System.out.println(lastName); System.out.println(age); Ma
🌐
Jenkov
jenkov.com › tutorials › java-json › gson-jsonparser.html
GSON - JsonParser
January 27, 2021 - JsonParser parser = new JsonParser(); String json = "{ \"f1\":\"Hello\",\"f2\":{\"f3\":\"World\"}}"; JsonElement jsonTree = parser.parse(json); if(jsonTree.isJsonObject()){ JsonObject jsonObject = jsonTree.getAsJsonObject(); JsonElement f1 = jsonObject.get("f1"); JsonElement f2 = jsonObject.get("f2"); if(f2.isJsonObject()){ JsonObject f2Obj = f2.getAsJsonObject(); JsonElement f3 = f2Obj.get("f3"); } }
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-jsonparser.html
Jackson JsonParser
June 6, 2015 - The Jackson JsonParser is a low level JSON parser which is therefore faster, but also more cumbersome to work with than the Jackson ObjectMapper. This tutorial explains how to use the Jackson JsonParser - for when you need fast JSON parsing.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › json
Java JSON parser Example - Java Code Geeks
September 24, 2021 - Now let’s explain the code above. 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. To retrieve primitive objects, get() method of the JSONObject's instance is called, defining the specified key as an argument.
Find elsewhere
🌐
HowToDoInJava
howtodoinjava.com › home › gson › gson jsonparser
Gson JsonParser (with Examples) - HowToDoInJava
April 4, 2023 - Gson JsonParser is used to parse Json data into a parse tree of JsonElement and thus JsonObject. JsonObject can be used to get access to the values using corresponding keys in JSON string.
🌐
Oracle
docs.oracle.com › javame › 8.0 › api › json › api › com › oracle › json › stream › JsonParser.html
JsonParser (JSON Documentation)
The returned value is equal to intValue(). Note that this conversion can lose information about the overall magnitude and precision of the number value as well as return a result with the opposite sign. This method should only be called when the parser state is JsonParser.Event.VALUE_NUMBER.
🌐
Oracle
oracle.com › java › technical details
Java API for JSON Processing
1 URL url = new URL("https://graph.facebook.com/search?q=java&type=post"); 2 try (InputStream is = url.openStream(); 3 JsonParser parser = Json.createParser(is)) { 4 while (parser.hasNext()) { 5 Event e = parser.next(); 6 if (e == Event.KEY_NAME) { 7 switch (parser.getString()) { 8 case "name": 9 parser.next(); 10 System.out.print(parser.getString()); 11 System.out.print(": "); 12 break; 13 case "message": 14 parser.next(); 15 System.out.println(parser.getString()); 16 System.out.println("---------"); 17 break; 18 } 19 } 20 } 21 }
🌐
MangoHost
mangohost.net › mangohost blog › jackson json java parser api – example tutorial
Jackson JSON Java Parser API – Example Tutorial
August 3, 2025 - // Jackson's internal flow simplified JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(jsonString); ObjectMapper mapper = new ObjectMapper(); // 1. Tokenization - breaks JSON into tokens // 2. Type detection - determines target Java types // 3.
🌐
Baeldung
baeldung.com › home › json › using static methods instead of deprecated jsonparser
Using Static Methods Instead of Deprecated JsonParser | Baeldung
June 20, 2024 - In this tutorial, we’ll delve into how to utilize the static methods instead of the deprecated JsonParser for efficient JSON parsing in Java.
🌐
JAXB
javaee.github.io › javaee-spec › javadocs › javax › json › stream › JsonParser.html
JsonParser (Java(TM) EE 8 Specification APIs)
Returns a String for the name in a name/value pair, for a string value or a number value. This method should only be called when the parser state is JsonParser.Event.KEY_NAME, JsonParser.Event.VALUE_STRING, or JsonParser.Event.VALUE_NUMBER.
🌐
TutorialsPoint
tutorialspoint.com › home › jackson › jackson jsonparser
Jackson JSONParser - Efficient JSON Processing in Java
January 11, 2015 - Learn how to use Jackson JSONParser for efficient JSON processing in Java. Explore its features, methods, and examples.
🌐
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
🌐
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 ...
🌐
LambdaTest Community
community.lambdatest.com › general discussions
How can I parse JSON in Java? - LambdaTest Community
June 9, 2024 - 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", ...
🌐
Jakarta EE
jakarta.ee › learn › docs › jakartaee-tutorial › current › web › jsonp › jsonp.html
JSON Processing :: Jakarta EE Tutorial :: Jakarta EE Documentation
See the Jakarta EE API reference for the jakarta.json.stream.JsonParser interface for more information. The output of this example is the following: START_OBJECT KEY_NAME firstName - VALUE_STRING Duke KEY_NAME lastName - VALUE_STRING Java KEY_NAME age - VALUE_NUMBER 18 KEY_NAME streetAddress - VALUE_STRING 100 Internet Dr KEY_NAME city - VALUE_STRING JavaTown KEY_NAME state - VALUE_STRING JA KEY_NAME postalCode - VALUE_STRING 12345 KEY_NAME phoneNumbers - START_ARRAY START_OBJECT KEY_NAME type - VALUE_STRING mobile KEY_NAME number - VALUE_STRING 111-111-1111 END_OBJECT START_OBJECT KEY_NAME type - VALUE_STRING home KEY_NAME number - VALUE_STRING 222-222-2222 END_OBJECT END_ARRAY END_OBJECT ·