This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

How do I parse JSON in Java?

I have the following JSON text. How can you parse it to get the values 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"}}]Copy the code

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

For illustration purposes, suppose you have a Person that contains only one class name.

private class Person { public String name;

public Person(String name) {
    this.name = name;
}
Copy the code

}

Google GSON (Maven)

My personal favorite object is the excellent JSON serialization/deserialization.

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"}
Copy the code

update

If you want to get a single attribute, you can easily do so using the Google library:

JsonObject jsonObject = new JsonParser().parse("{\"name\": \"John\"}").getAsJsonObject(); System.out.println(jsonObject.get("name").getAsString()); //John org. JSON (Maven)Copy the code

If you don’t need object deserialization and just want to get properties, try org.json (or check out the GSON example above!).

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

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

Jackson (Maven)

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

System.out.println(user.name); //John
Copy the code

The article translated from yl2gl72eozkinivz3vc6swkesy – ac4c6men2g7xr2a – translate. Translate. Goog/questions / 2…

Gson can support three layers of nesting, Jackson community is active, Fastjson is very popular in China, but there are also shortcomings, I see ali cloud is not using Fastjson to parse JSON

The reason WHY I know ali cloud is not using Fastjson to parse JSON is because, I docking nail API document, in the source code does not appear fastjson figure, if you are interested, but PRIVATE message I want to source!


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️