-4

I am returning json as shown below

[
   {
      "data":{
         "Id":"3",
         "Name":"Harry",
         "syncsts":"0",
         "update_at":"2016-10-04 10:30:48"
      }
   },
   {
      "data":{
         "Id":"2",
         "Name":"Howard",
         "syncsts":"0",
         "update_at":"2016-10-04 10:29:26"
      }
   },
   {
      "data":{
         "Id":"1",
         "Name":"Brady",
         "syncsts":"0",
         "update_at":"2016-10-04 10:29:26"
      }
   }
]

I am trying to parse this json in Android using GSON

nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • Welcome to StackOverflow. Please read the "[how to ask a question](http://stackoverflow.com/questions/ask)" section before posting a question. Your question already has a lot of answers on stackoverflow itself. Please do so research before posting these questions. Should you feel your question is a bit different from those already asked, edit your question to let us know. – Sajib Acharya Oct 03 '16 at 20:33
  • Also, here are some resource links to get you started with your question: [link one](http://stackoverflow.com/questions/ask), [link two](http://blog.nkdroidsolutions.com/how-to-parsing-json-array-using-gson-in-android-tutorial/). – Sajib Acharya Oct 03 '16 at 20:34

1 Answers1

0

You need to create Model classes:

public class Data {

@SerializedName("Id")
@Expose
private String id;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("syncsts")
@Expose
private String syncsts;
@SerializedName("update_at")
@Expose
private String updateAt;

/**
* 
* @return
* The id
*/
public String getId() {
return id;
}

/**
* 
* @param id
* The Id
*/
public void setId(String id) {
this.id = id;
}

/**
* 
* @return
* The name
*/
public String getName() {
return name;
}

/**
* 
* @param name
* The Name
*/
public void setName(String name) {
this.name = name;
}

/**
* 
* @return
* The syncsts
*/
public String getSyncsts() {
return syncsts;
}

/**
* 
* @param syncsts
* The syncsts
*/
public void setSyncsts(String syncsts) {
this.syncsts = syncsts;
}

/**
* 
* @return
* The updateAt
*/
public String getUpdateAt() {
return updateAt;
}

/**
* 
* @param updateAt
* The update_at
*/
public void setUpdateAt(String updateAt) {
this.updateAt = updateAt;
}

}

and:

public class Example {

@SerializedName("data")
@Expose
private Data data;

/**
* 
* @return
* The data
*/
public Data getData() {
return data;
}

/**
* 
* @param data
* The data
*/
public void setData(Data data) {
this.data = data;
}

}

and:

and then parse your json string using GSON :

Gson gson = new Gson();
List<Data> data = new ArrayList<>();
//then create a loop where you put your gson in list
//for example
for(Object obj : list) {
 Data data = gson.fromJson(obj, Data.class);
data.add(data);
}
VLeonovs
  • 2,193
  • 5
  • 24
  • 44