Android 使用GSon如何从JSONObject获取多个值

Android 使用GSon如何从JSONObject获取多个值,android,json,gson,Android,Json,Gson,您好,下面是我对format的回复 object {6} csdfgi_fgid : 4casdff9743a1-3f9asdf2-4 fesdfgedVsdfgersionsfdg : 7 sdfg : 28 feesdfgdStart : 0 fesdfedCsdfgoudfsgnt : 28 discover[28] 0{4} DiscCat : Tip templateCat{2} name

您好,下面是我对format的回复

object {6}
  csdfgi_fgid   :   4casdff9743a1-3f9asdf2-4
  fesdfgedVsdfgersionsfdg   :   7
  sdfg  :   28
  feesdfgdStart :   0
  fesdfedCsdfgoudfsgnt  :   28
  discover[28]
   0{4}
    DiscCat :   Tip
    templateCat{2}
        name    :   tip.tp
        version :   2
    timestamp   :   1421146871251
    content{1}
我在类中使用Serializable获得这些值,如下所示

@SerializedName("csdfgi_fgid")
public String sCdfsisdfId;

@SerializedName("feedVersion")
public Long lFesdfgedVsdfgfsersdfsion;

@SerializedName("feed")
public ArrayList<DiscoverModel> discover;
@SerializedName(“csdfgi_fgid”)
公共字符串SCDFISDFID;
@SerializedName(“feedVersion”)
公共长期基金;
@序列化名称(“提要”)
公共阵列列表发现;

现在在数组列表中还有一个Json对象“templateCat”,它有两个参数“name,version”。现在,如何从这个JSON对象中获取值,您必须首先获取JSON数组,遍历它,然后使用Gson将其转换为所需的对象

JSONObject rootJson = /* Get your JSON object here */;
JSONArray nestedArray = rootJson.getJsonArray("your_nested_json_array_name");

List<YourObject> objects;
for(int i = 0; i < nestedArray.size(); i++) {
    String objectJsonString = nestedArray.get(i);
    YourObject o = new Gson().fromJson(objectJsonString, YourObject.class);

    objects.add(o);
}
JSONObject rootJson=/*在此处获取JSON对象*;
JSONArray nestedArray=rootJson.getJsonArray(“您的嵌套json数组名称”);
列出对象;
对于(int i=0;i

…您需要做的最后一件事是将
对象
列表设置为所需的实例。

首先,您需要分配POJO类中获得的所有JSONObject。在从JSONArray(discover)获取JSONObject(templateCat)之后,您必须进行循环。例如:

for(int i=0;i<discover.size();i++){

   TemplateCat templateCat = discover.get(i).getTemplateCat();
   String strName = templateCat.getName();
   String strVersion = templateCat.getVersion();

  //If you want to get all name and version as list. Add strName and strVersion to ArrayList. <br>

    For Example :  nameArray.add(strName);
                   versionArray.add(strVersion);
}
for(int i=0;i这里有个问题(您已经尝试了什么)?root->discover[i]->templateCat