All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Parse JSON In Java

Last Updated : Mar 11, 2024

How To Parse JSON In Java

In this article we will show you the solution of how to parse JSON in java, JSON (JavaScript Object Notation), a lightweight, text-based, language-independent data exchange format, is simple to read and write for both humans and machines.

Objects and arrays are the two structured types that JSON can represent. An object is a collection of 0 or more name/value pairs. 0 or more ordered values can be found in an array.

Strings, numbers, Booleans, null, and these 2 structured types can all be used as values.

Here is a straightforward Wikipedia example that illustrates how to represent a person-descriptive object in JSON.

The object contains string values for the person's first and last name, a number valuation for age, an object valuation representing the person's address, and an array of contact information objects.

JSON Processing in Java: The JSON Processing API in Java. simple is a straightforward Java library that allows you to parse, generate, transform, and query JSON.

Starting Out: The json-simple-1.1 jar file must be downloaded and added to your CLASSPATH before compiling and running the example codes below.

If you are using Maven, please visit https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1.

It is widely used during web programming as a data-exchange format. The org.json library is used to parse JSON in this example.

JSON objects are unsorted collections of key/value pairs. JSON arrays are collections of values in a particular order. Objects or arrays could be used as values.

As an example, we'll parse this JSON to get values for pageName, pagePic, and post id.

{
    "pageInfo": {
            "pageName": "Homepage",
            "logo": "https://www.example.com/logo.jpg"
    },
    "posts": [
            {
                "post_id": "0123456789",
                "actor_id": "1001",
                "author_name": "Jane Doe",
                "post_title": "How to parse JSON in Java",
                "comments": [],
                "time_of_post": "1234567890"
            }
    ]
}

Step By Step Guide On How To Parse JSON In Java :-

import org.json.JSONArray;
import org.json.JSONObject;
public class ParseJSON {
    static String json = " ";
    public static void main(String[] args) {
        JSONObject obj = new JSONObject(json);
        String pageName = obj.getJSONObject("pageInfo").getString("pageName");
        System.out.println(pageName);
        JSONArray arr = obj.getJSONArray("posts");
        for (int i = 0; i < arr.length(); i++) {
            String post_id = arr.getJSONObject(i).getString("post_id");
            System.out.println(post_id);
        }
    }
}
  1. First, we must use the JSONObject class to transform this same JSON string into the a JSON Object.
  2. Because "pageInfo" is a JSON Object, we just use getJSONObject method.
  3. Similarly, because "posts" is a JSON Array, we must apply the getJSONArray method.

Conclusion :-

Simple JSON API: It provides JSON object as well as array structure object models.

JSON structures are object models that utilize the JSONObject as well as JSONArray forms.

To access this same model's unsorted collection of zero or so more name/value pairs, JSONObject gives a Google map.

JSONArray, similarly, does provide a View button to connect the model's answers to the research questions of 0 or more values.

I hope this article on how to parse JSON in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪