Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+10 votes
5 views
in Java by (1k points)
edited by

In java, how can I parse json text to get value of variables like post_id ?

Here is my json text 

  "page": {
         "name": "tommas",
    },
    "posts": [
         {
              "post_id": "123456789012_123456789012",
              "actor_id": "1234567890",
              "name": "Alex",
              "message": "Nice!",
              "likes_count": "3",
              "time": "1234567890"
         }
    ]
}

2 Answers

0 votes
by (13.2k points)

JSON basically stands for JavaScript Object Notation. It is simply a format in which in which you can transfer data from client to server and vice versa. In the initial days of web development we used to work with HTML later with CSS as it all evolved, as it evolved we had more dynamic web pages, what it meant was we had different operating languages on both ends and complex strings which need to be communicated needed to be transferable to both ends, which is when JSON came in. JSON is a lightweight, text-based, language-independent data exchange format which is used for the purpose.

Parsing JSON in java

We assume here we want to parse some data from server side to client side. Passing JSON is much easier if let’s say we are operating in JavaScript, but if we are using it in some other language ( Java in our case) we need to take help of certain libraries in order to convert normal text to JSON format first on the client side and parse it to client side and again convert it to normal text using libraries. Library which is commonly used are gson, jackson  all these are available and can be used to convert java code into Json and vice versa. We will use org.json library here,

import org.json.*;

JSONObject obj = new JSONObject(" .... ");

String name = obj.getJSONObject("page").getString("name");

JSONArray arr = obj.getJSONArray("posts");

for (int i = 0; i < arr.length(); i++)

 {

   String id = arr.getJSONObject(i).getString("post_id");

   ......

 }

0 votes
by (32.3k points)

Let's assume you have a class called Person with just name as an attribute.

For example:

private class Person {

    public String name;

              public Person(String name) {

        this.name = name;

    }

}

Do you want to learn Java end to end! Here's the right video for you:

For the serialization/de-serialization of objects, you can do this:

Gson g = new Gson();

Person person = g.fromJson("{\"name\": \"John\"}", Person.class);

System.out.println(person.name); //John

System.out.println(g.toJson(person)); // {"name":"John"}

If you want to get a single attribute out you can do it easily with the Google library as well, do like this:

JsonObject jsonObject = new JsonParser().parse("{\"name\": \"John\"}").getAsJsonObject();

System.out.println(jsonObject.get("name").getAsString()); 

But, if you don't need object de-serialization but to simply get an attribute, you can try org.json 

JSONObject obj = new JSONObject("{\"name\": \"John\"}");

System.out.println(obj.getString("name")); 

ObjectMapper mapper = new ObjectMapper();

Person user = mapper.readValue("{\"name\": \"John\"}", Person.class);

System.out.println(user.name); 

Related questions

+2 votes
2 answers
0 votes
1 answer
asked Oct 27, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
asked Jul 3, 2019 in BI by Vaibhav Ameta (17.6k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...