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
🌐
GitHub
github.com › HoussemLahiani › JSONParser › blob › master › JSONParser.java
JSONParser/JSONParser.java at master · HoussemLahiani/JSONParser
import java.util.HashMap; · · public class JSONParser { · · String charset = "UTF-8"; · HttpURLConnection conn; · DataOutputStream wr; · StringBuilder result; · URL urlObj; · JSONObject jObj = null; · StringBuilder sbParams; ·
Author   HoussemLahiani
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › json
Java JSON parser Example - Java Code Geeks
September 24, 2021 - package com.javacodegeeks.javabasics.jsonparsertest; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class JsonParseTest { private static final String filePath = "jsonTestFile.json"; public static void main(String[] args) { try (FileReader reader = new FileReader(ClassLoader.getSystemResource(filePath).getFile())) { // read the json file JSONParser jsonParser =
🌐
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
🌐
Tabnine
tabnine.com › home page › code › java › org.json.simple.parser.jsonparser
org.json.simple.parser.JSONParser.parse java code examples | Tabnine
August 7, 2019 - JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); // get a String from the JSON object String firstName = (String) jsonObject.get("firstname"); System.out.println("The first name is: " + ...
🌐
JAXB
javaee.github.io › javaee-spec › javadocs › javax › json › stream › JsonParser.html
JsonParser (Java(TM) EE 8 Specification APIs)
JsonParser can be used to parse sequence of JSON values that are not enclosed in a JSON array, e.g.
🌐
javaspring
javaspring.net › blog › how-to-import-jsonparser-in-java
How to Import and Use JsonParser in Java — javaspring.net
June 17, 2015 - When working with resources like JsonParser, it is recommended to use the try - with - resources statement. This ensures that the resources are automatically closed after use, even if an exception occurs. As shown in the JsonFileParserExample above. Separate the JSON parsing logic into smaller, reusable methods. This makes your code more maintainable and easier to test. import javax.json.Json; import javax.json.stream.JsonParser; import javax.json.stream.JsonParser.Event; import java.io.FileInputStream; import java.io.IOException; public class ModularCodeExample { public static void main(Strin
Find elsewhere
🌐
Javadoc.io
javadoc.io › doc › com.google.code.gson › gson › 2.6.2 › com › google › gson › JsonParser.html
JsonParser - gson 2.6.2 javadoc
Bookmarks · Latest version of com.google.code.gson:gson · https://javadoc.io/doc/com.google.code.gson/gson · Current version 2.6.2 · https://javadoc.io/doc/com.google.code.gson/gson/2.6.2 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/com.goog...
🌐
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
🌐
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.
🌐
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('{'), ...
🌐
HowToDoInJava
howtodoinjava.com › home › gson › gson jsonparser
Gson JsonParser (with Examples) - HowToDoInJava
April 4, 2023 - Java program to parse JSON into JsonElement (and JsonObject) using JsonParser and fetch JSON values using keys. import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class JsonElementExample { public static void main(String[] args) { String json = "{'id': 1001, " + "'firstName': 'Lokesh'," + "'lastName': 'Gupta'," + "'email': 'howtodoinjava@gmail.com'}"; JsonElement jsonElement = JsonParser.parseString(json); JsonObject jsonObject = jsonElement.getAsJsonObject(); System.out.println( jsonObject.get("id") ); System.out.println( jsonObject.get("firstName") ); System.out.println( jsonObject.get("lastName") ); System.out.println( jsonObject.get("email") ); } }
🌐
TutorialsPoint
tutorialspoint.com › home › json_simple › json simple quick guide
JSON Simple Quick Guide
March 14, 2013 - import java.io.IOException; import java.util.List; import java.util.Stack; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.ContentHandler; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; class JsonDemo { public static void main(String[] args) { JSONParser parser = new JSONParser(); String text = "{\"first\": 123, \"second\": [4, 5, 6], \"third\": 789}"; try { CustomContentHandler handler = new CustomContentHandler(); parser.parse(text, handler,true); } catch(ParseException pe) { } } } class CustomContent
🌐
Google
docs.cloud.google.com › java › client libraries › class jsonparser (2.0.2)
Class JsonParser (2.0.2) | Java client libraries | Google Cloud Documentation
April 23, 2024 - Beta Parse a JSON object, array, or value into a new instance of the given destination class using <xref uid="com.google.api.client.json.JsonParser.
🌐
TutorialsPoint
tutorialspoint.com › json › json_java_example.htm
JSON with Java
The following example makes use of JSONObject and JSONArray where JSONObject is a java.util.Map and JSONArray is a java.util.List, so you can access them with standard operations of Map or List. import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org.json.simple.parser.ParseException; import org.json.simple.parser.JSONParser; class JsonDecodeDemo { public static void main(String[] args) { JSONParser parser = new JSONParser(); String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; try{ Object obj = parser.parse(s); JSONArray array = (JSONArray)obj; System.out.pr
🌐
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.
🌐
DevQA
devqa.io › how-to-parse-json-in-java
How to Parse JSON in Java
November 8, 2019 - In order to use Gson to parse JSON in Java, you need to add the library as a dependency. You can get the latest version from Maven repository · The below example shows how to Parse the above JSON with Gson. import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class ParseJSON { static String json = "..."; public static void main(String[] args) { JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject(); String pageName = jsonObject.getAsJsonObject("pageInfo").get("pageName").getAsString(); System.out.println(pageName); JsonArray arr = jsonObject.getAsJsonArray("posts"); for (int i = 0; i < arr.size(); i++) { String post_id = arr.get(i).getAsJsonObject().get("post_id").getAsString(); System.out.println(post_id); } } } Like the previous example, the ...
🌐
DZone
dzone.com › coding › java › how to read and parse a json file in java
How to Read and Parse a JSON File in Java
March 6, 2025 - Java · //program for reading a JSON file import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.*; public class JSON { public static void main(Strings args[]) { // file name is File.json Object o = new JSONParser().parse(new FileReader(File.json)); JSONObject j = (JSONObject) o; String Name = (String) j.get(“Name”); String College = (String ) j.get(“College”); System.out.println(“Name :” + Name); System.out.println(“College :” +College); } } Output: Java ·