How to parse JSON in Java

I have the following JSON text. How can I parse it to get pageName , pagePic , post_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"
         }
    ]
}

The org.json library is easy to use. Example code below:

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

You may find extra examples from: Parse JSON in Java

Downloadable jar: http://mvnrepository.com/artifact/org.json/json


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

  • If one wants to create JAVA object from JSON and vice versa, use GSON or JACKSON third party jars etc.

    //from object to JSON 
    Gson gson = new Gson();
    gson.toJson(yourObject);
    
    // from JSON to object 
    yourObject o = gson.fromJson(JSONString,yourObject.class);
    
  • But if one just want to parse a JSON string and get some values, (OR create a JSON string from scratch to send over wire) just use JaveEE jar which contains JsonReader, JsonArray, JsonObject etc. You may want to download the implementation of that spec like javax.json. With these two jars I am able to parse the json and use the values.

    These APIs actually follow the DOM/SAX parsing model of XML.

    Response response = request.get(); // REST call 
        JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
        JsonArray jsonArray = jsonReader.readArray();
        ListIterator l = jsonArray.listIterator();
        while ( l.hasNext() ) {
              JsonObject j = (JsonObject)l.next();
              JsonObject ciAttr = j.getJsonObject("ciAttributes");
    
  • 链接地址: http://www.djcxy.com/p/5086.html

    上一篇: 内联显示无序列表,但保留子弹

    下一篇: 如何解析Java中的JSON