嵌套的Json与GSON解析 [英] Nested Json parsing with GSon

查看:211
本文介绍了嵌套的Json与GSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何分析以下与谷歌GSON JSON响应?

  {
   rootobject:[
      {
         ID:7
         名称:PP-1,
         副标题:名-I,
         KEY1:旁遮普
         key12:2013
         位置:,
         key13:0,
         key14:0,
         key15:0,
         result_status:空
      },
      {
         ID:7
         名称:PP-1,
         副标题:名-I,
         KEY1:旁遮普
         key12:2013
         位置:,
         key13:0,
         key14:0,
         key15:0,
         result_status:空
      },
      {
         ID:7
         名称:PP-1,
         副标题:名-I,
         KEY1:旁遮普
         key12:2013
         位置:,
         key13:0,
         key14:0,
         key15:0,
         result_status:空
      },
      {
         ID:7
         名称:PP-1,
         副标题:名-I,
         KEY1:旁遮普
         key12:2013
         位置:,
         key13:0,
         key14:0,
         key15:0,
         result_status:空
      }
   ]
}


解决方案

我想创建对象,以包装的反应,如:

 公共类响应{  @SerializedName(root_object)
  私人列表< YourObject> rootObject;  // getter和setter
}
公共类YourObject {  @SerializedName(ID)
  私人字符串ID;
  @SerializedName(名称)
  私人字符串名称;
  @SerializedName(副标题)
  私人字符串字幕;
  // ...等领域  // getter和setter
}

请注意:使用@SerializedName注释遵循命名约定在您的Java属性,而在JSON数据相匹配的名字

然后你只需解析与效应初探对象JSON,像这样的:

 字符串jsonString =你的JSON数据......;
GSON GSON =新GSON();
响应响应= gson.fromJson(jsonString,Response.class);

现在你可以使用getter和setter访问你的响应对象的所有数据。

请注意:你的响应对象可能被用来分析不同的JSON响应。例如,你可以有不包含 ID 字幕字段JSON响应,但是你的效应初探对象将解析响应,以及​​,只是把在这个领域。这样你就可以只用一个响应类来分析所有可能的对策......

编辑:我不知道Android的标签,我用这个方法在通常的Java程序,我不知道它是否是有效的为Android ...

How to parse below Json Response with google Gson.?

{
   "rootobject":[
      {
         "id":"7",
         "name":"PP-1",
         "subtitle":"name-I",
         "key1":"punjab",
         "key12":"2013",
         "location":"",
         "key13":"0",
         "key14":"0",
         "key15":"0",
         "result_status":null
      },
      {
         "id":"7",
         "name":"PP-1",
         "subtitle":"name-I",
         "key1":"punjab",
         "key12":"2013",
         "location":"",
         "key13":"0",
         "key14":"0",
         "key15":"0",
         "result_status":null
      },
      {
         "id":"7",
         "name":"PP-1",
         "subtitle":"name-I",
         "key1":"punjab",
         "key12":"2013",
         "location":"",
         "key13":"0",
         "key14":"0",
         "key15":"0",
         "result_status":null
      },
      {
         "id":"7",
         "name":"PP-1",
         "subtitle":"name-I",
         "key1":"punjab",
         "key12":"2013",
         "location":"",
         "key13":"0",
         "key14":"0",
         "key15":"0",
         "result_status":null
      }
   ]
}

解决方案

I'd create objects to "wrap" the response, such as:

public class Response {

  @SerializedName("root_object")
  private List<YourObject> rootObject;

  //getter and setter
}


public class YourObject {

  @SerializedName("id")
  private String id;
  @SerializedName("name")
  private String name;
  @SerializedName("subtitle")
  private String subtitle;
  //... other fields

  //getters and setters
}

Note: use @SerializedName annotation to follow naming conventions in your Java attribute while matching the names in the JSON data.

Then you just parse the JSON with your Reponse object, like this:

String jsonString = "your json data...";
Gson gson = new Gson();
Response response = gson.fromJson(jsonString, Response.class);

Now you can access all the data in your Response object using getters and setters.

Note: your Response object may be used to parse different JSON responses. For example you could have JSON response that don't contain the id or the subtitle fields, but your Reponseobject will parse the response as well, and just put a null in this fields. This way you can use only one Responseclass to parse all the possible responses...

EDIT: I didn't realise the Android tag, I use this approach in a usual Java program, I'm not sure whether it's valid for Android...

这篇关于嵌套的Json与GSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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