Nested Json parsing with GSon

2019-07-25 07:32发布

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

2条回答
迷人小祖宗
2楼-- · 2019-07-25 07:58

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...

查看更多
Lonely孤独者°
3楼-- · 2019-07-25 08:06

You can try this hope this will work

 // Getting Array 
JSONArray contacts = json.getJSONArray("rootobject");
SampleClass[] sample=new SampleClass[contacts.length]();

    // looping through All 
    for(int i = 0; i < contacts.length(); i++){
        JSONObject c = contacts.getJSONObject(i);

        // Storing each json item in variable
        sample[i].id = c.getString("id");
        sample[i].name = c.getString("name");
        sample[i].email = c.getString("subtitle");
        sample[i].address = c.getString("key1");
        sample[i].gender = c.getString("key12");
        sample[i].gender = c.getString("location");
        sample[i].gender = c.getString("key13");
        sample[i].gender = c.getString("key14");
        sample[i].gender = c.getString("key15");
        sample[i].gender = c.getString("result_status");
       }
查看更多
登录 后发表回答