You can using Gson library. if we assume your object is a person class like this
class Person{
String name;
int age;
}
and your json response like this
[
{
"OBJECT1":{
"name":"mohamed",
"age":21
},
"OBJECT2":{
"name":"shalan",
"age":21
}
}
]
you need to create a class to reflect each object in your json
for Object1
public class OBJECT1 {
@SerializedName("name")
@Expose
private String name;
@SerializedName("age")
@Expose
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
and for your Object2
public class OBJECT2 {
@SerializedName("name")
@Expose
private String name;
@SerializedName("age")
@Expose
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
and this is a person class for holding your objects
public class Person {
@SerializedName("OBJECT1")
@Expose
private OBJECT1 oBJECT1;
@SerializedName("OBJECT2")
@Expose
private OBJECT2 oBJECT2;
public OBJECT1 getOBJECT1() {
return oBJECT1;
}
public void setOBJECT1(OBJECT1 oBJECT1) {
this.oBJECT1 = oBJECT1;
}
public OBJECT2 getOBJECT2() {
return oBJECT2;
}
public void setOBJECT2(OBJECT2 oBJECT2) {
this.oBJECT2 = oBJECT2;
}
}
to convert your json array to a list of your object List
Gson gson = new Gson();
String jsonOutput = "your json result is here";
Type listType = new TypeToken<List<Person>>(){}.getType();
List<Persopn> persons = (List<Person>) gson.fromJson(jsonOutput, listType);