I want to parse the JSON code in Java, how do I do it?

Asked 2 years ago, Updated 2 years ago, 101 views

I want to get the values of pageName, pagePic, post_id, etc. from the json code below, what should I do??

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

json java parsing

2022-09-22 16:13

1 Answers

org.jsonUse the library to make it easy.

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");
    ......
}

For other examples, go to the link .


2022-09-22 16:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.