complex json to java object conversion using gson

2019-09-22 08:30发布

问题:

This is my json need to be converted to Java object. I have tried many method but not got sucess so if any one can provide suggession on this are most welcome!

{
  "id":"100006077890894",
  "posts":{
    "data":[
      {
        "message":"this is my 4th post to test...((((((((((((((((",
        "from":{
          "name":"Sagar Zope",
          "id":"100006077890894"
        },
        "id":"100006077890894_1384558691756714",
        "created_time":"2013-06-12T07:02:52+0000",
        "comments":{
          "data":[
            {
              "from":{
                "name":"Sagar Zope",
                "id":"100006077890894"
              },
              "message":"4th post 1st comment ..............",
              "id":"1384558691756714_10803"
            },
            {
              "from":{
                "name":"Sagar Zope",
                "id":"100006077890894"
              },
              "message":"4th post 2nd comment ..............",
              "id":"1384558691756714_10804"
            },
            {
              "from":{
                "name":"Sagar Zope",
                "id":"100006077890894"
              },
              "message":"4th post 3rd comment ..............",
              "id":"1384558691756714_10805"
            },
            {
              "from":{
                "name":"Sagar Zope",
                "id":"100006077890894"
              },
              "message":"4th post 4th comment ..............",
              "id":"1384558691756714_10806"
            }
          ]
        }
      },
      {
        "message":"this is the 3rd post .....................:)))))))",
        "from":{
          "name":"Sagar Zope",
          "id":"100006077890894"
        },
        "id":"100006077890894_1384557311756852",
        "created_time":"2013-06-12T07:01:24+0000",
        "comments":{
          "data":[
            {
              "from":{
                "name":"Sagar Zope",
                "id":"100006077890894"
              },
              "message":"3rd post 1st comment ..............",
              "id":"1384557311756852_10797"
            },
            {
              "from":{
                "name":"Sagar Zope",
                "id":"100006077890894"
              },
              "message":"3rd post 2nd comment ..............",
              "id":"1384557311756852_10800"
            },
            {
              "from":{
                "name":"Sagar Zope",
                "id":"100006077890894"
              },
              "message":"3rd post 3rd comment ..............",
              "id":"1384557311756852_10801"
            },
            {
              "from":{
                "name":"Sagar Zope",
                "id":"100006077890894"
              },
              "message":"3rd post 4th comment ..............",
              "id":"1384557311756852_10802"
            }
          ]
        }
      }
    ]
  }
}

回答1:

You need a class structure like this (pseudo-code):

class Response
  String id
  DataList posts

class DataList
  List<Data> data

class Data
  String message
  From from
  String id
  String created_time
  DataList comments

class From
  String name
  String id

Note that you can change class names if you want, but you have to keep the attribute names to match the field names in the JSON response.

Also note that I've used the same class Data to store the data for the posts and for the comments. This is possible because the data for comments is a subset of the data for posts, so when parsing a comment, Gson will just ignore the attributes created_time and comments...

Then parse the JSON with:

Gson gson = new Gson();
Response response = gson.fromJson(yourJsonString, Response.class);

Now you can get the i post with:

String postMessage = response.getPosts().getData().get(i).getMessage();

And in the same way you can get its comments with:

String commentMessage = response.getPosts().getData().get(i).getComments().getData().get(i).getMessage();

NOTE: Using an Online JSON Viewer will help you to clearly see the class structure you need to wrap your JSON data!



标签: java json gson