How to parse JSON in Java

0 votes

I have the following JSON text. How can I parse it to get pageNamepagePicpost_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. Can't wait to see it!",
              "likesCount": "2",
              "comments": [],
              "timeOfPost": "1234567890"
         }
    ]
}
Apr 18, 2018 in Java by developer_1
• 3,280 points
1,154 views

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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;
    }
}

Google GSON (Maven)

My personal favourite as to the great JSON serialisation / de-serialisation of objects.

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"}

Update

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

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

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

Org.JSON (Maven)

If you don't need object de-serialisation but to simply get an attribute, you can try org.json (or look GSON example above!)

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

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

Jackson (Maven)

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

System.out.println(user.name); //John
answered Apr 18, 2018 by Rishabh
• 3,520 points
0 votes

We can use the JSONObject class to convert a JSON string to a JSON object and to iterate over the JSON object. Use the following code.

JSONObject jObj = new JSONObject(contents.trim());
Iterator<?> keys = jObj.keys();

while( keys.hasNext() ) {
  String key2 = (String)keys.next();
  if ( jObj.get(key2) instanceof JSONObject ) {           
    System.out.println(jObj.getString(String key));
  }
}
answered Jul 31, 2018 by samarth295
• 2,190 points
0 votes
import org.json.*;


JSONObject obj = new JSONObject(" .... ");
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
}
answered Aug 20, 2018 by Daisy
• 8,040 points

Related Questions In Java

+14 votes
17 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7 in Java by Suresh
• 700 points
57,474 views
0 votes
1 answer

How to convert a JSON String into Object in Java?

We can easily convert a JSON String ...READ MORE

answered Jun 8, 2018 in Java by parth295
• 4,600 points
179 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,150 points
1,401 views
0 votes
2 answers

How do we convert JSON to Map in Java

When you don't know structure of json. ...READ MORE

answered Oct 31, 2018 in Java by Sushmita
• 6,840 points
114 views
0 votes
1 answer

How can we get file extension in Java?

To get the file extension, we can ...READ MORE

answered May 8, 2018 in Java by parth295
• 4,600 points
203 views
0 votes
1 answer

How to read text files from the Classpath in Java?

InputStream in = this.getClass().getClassLoader().getResourceAsStream("TextFile.txt"); InputStream in = this.getClass().getResourceAsStream("/TextFile.txt"); package ...READ MORE

answered May 8, 2018 in Java by Akrati
• 3,150 points
105 views
0 votes
1 answer

How to read and write on an excel files?

Follow these steps: Write: public class User { ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,520 points
153 views
0 votes
1 answer

Access restriction on class error

My guess is that you are trying ...READ MORE

answered Jun 21, 2018 in Java by Rishabh
• 3,520 points
32 views
0 votes
1 answer

How to pad string in Java?

Padding to 10 characters: String.format("%10s", "foo").replace(' ', '*'); String.format("%-10s", ...READ MORE

answered Apr 30, 2018 in Java by Rishabh
• 3,520 points
34 views
0 votes
1 answer

How to calculate method execution time in Java ?

Use the following code : new Timer(""){{ ...READ MORE

answered May 24, 2018 in Java by Rishabh
• 3,520 points
83 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.