复杂的json使用gson进行java对象转换

时间:2013-06-12 11:49:50

标签: java json gson

这是我的json需要转换为Java对象。 我已经尝试了很多方法,但没有成功,所以如果有人可以提供这方面的建议是非常受欢迎的!

{
  "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 个答案:

答案 0 :(得分:1)

您需要这样的类结构(伪代码):

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

请注意,您可以根据需要更改类名,但必须保持属性名称以匹配JSON响应中的字段名称。

另请注意,我使用了相同的班级Data来存储帖子和评论的数据。这是可能的,因为评论数据是帖子数据的子集,因此在解析评论时,Gson将忽略属性created_timecomments ...

然后用:

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

现在您可以使用

获取 i 帖子
String postMessage = response.getPosts().getData().get(i).getMessage();

以同样的方式你可以得到它的评论:

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

注意:使用Online JSON Viewer将帮助您清楚地看到包装您的JSON数据所需的类结构!