复杂的json到使用gson的java对象转换 [英] complex json to java object conversion using gson

查看:199
本文介绍了复杂的json到使用gson的java对象转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  {
id:100006077890894,
posts:{
data:[
{
message:this is my 4th post ((((((((((((,
from):{
name:Sagar Zope,
id:100006077890894
},
id:100006077890894_1384558691756714,
created_time:2013-06-12T07:02:52 + 0000,
评论:{
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
},
$ bfrom:{
name:Sagar Zope,
id:100006077890894
},
消息: 4th post 3rd comment ..............,
id:1384558691756714_10805
},
{
from :{
name:Sagar Zope,
id:100006077890894
},
消息:4th post 4th comment ..... .........,
id:1384558691756714_10806
}
]
}
},
{
message:这是第三篇文章..................... :))))))),
from :{
name:Sagar Zope,
id:100006077890894
},
id:1 00006077890894_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
}
]
}




$ b

解决方案

您需要像这样的类结构( pseudo-code ):

  class Response 
String id
DataList帖子
$ b $ class DataList
List< Data>数据

类数据
字符串消息
来自
字符串ID
字符串created_time
DataList注释

类从
字符串名称
字符串标识

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



另外请注意,我使用了相同的类 Data 来存储帖子和评论的数据。这是可能的,因为注释数据是posts的数据的子集,所以当解析注释时,Gson将忽略属性 created_time 注释 ...



然后解析JSON:

  Gson gson = new Gson(); 
响应响应= 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(); 






注意:使用 在线JSON查看器 将帮助您清楚地看到您需要的类结构 wrap 你的JSON数据!

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

解决方案

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!

这篇关于复杂的json到使用gson的java对象转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆