Reputation: 55
How to Call dynamic nested json object in android using retrofit.I have a JSON result in the following format, My question is: how do I access the content of "categories" since "food", "fashion", etc are all dynamic values? just give me an idea.
{
"offer": {
"id": "JUN_HAIR_1302177631",
"categories": {
"electronics": {
"address_1": "12 Mott St",
"address_2": null,
"city": "New York",
"cross_streets": "Chatham Sq & Worth St",
"state": "NY",
"zip": "10013"
},
"food": {
"address_1": "12 Mott St",
"address_2": null,
"city": "New York",
"cross_streets": "Chatham Sq & Worth St",
"state": "NY",
"zip": "10013"
},
"fashion": {
"address_1": "12 Mott St",
"address_2": null,
"city": "New York",
"cross_streets": "Chatham Sq & Worth St",
"state": "NY",
"zip": "10013"
},
.........
.........
}
}
}
Upvotes: 0
Views: 579
Reputation: 524
This is list of models needed for that json, your models should be like this:
public class YourObject {
@SerializedName("offer")
private Offer offer;
}
public class Offer {
@SerializedName("id")
private String id;
@SerializedName("categories")
private Categories categories;
}
public class Categories {
@SerializedName("electronics")
private Electronics electronics;
@SerializedName("food")
private Food food;
@SerializedName("fashion")
private Fashion fashion;
}
public class Electronics {
@SerializedName("address_1")
private String address1;
@SerializedName("address_2")
private Object address2;
@SerializedName("city")
private String city;
@SerializedName("cross_streets")
private String crossStreets;
@SerializedName("state")
private String state;
@SerializedName("zip")
private String zip;
}
public class Fashion {
@SerializedName("address_1")
private String address1;
@SerializedName("address_2")
private Object address2;
@SerializedName("city")
private String city;
@SerializedName("cross_streets")
private String crossStreets;
@SerializedName("state")
private String state;
@SerializedName("zip")
private String zip;
}
public class Food {
@SerializedName("address_1")
private String address1;
@SerializedName("address_2")
private Object address2;
@SerializedName("city")
private String city;
@SerializedName("cross_streets")
private String crossStreets;
@SerializedName("state")
private String state;
@SerializedName("zip")
private String zip;
}
Upvotes: 1
Reputation: 1389
You can use HashMap
:
class Offer {
private String id;
private HashMap<String, Category> categories;
// getters and setters
}
And the Category
data class should look something like this:
class Category {
@SerializedName("address_1")
private String firstAddress;
@SerializedName("address_2")
private String secondAddress;
private String city;
@SerializedName("cross_streets")
private String crossStreets;
private String state;
private String zip;
// getters and setters;
}
Upvotes: 1
Reputation: 3404
Use Pojo to generate the class for your json.
Steps to follow
Go to package -> Create New -> Select Generate POJO from JSON
If the Option is not showing then you have not installed the plugin.
Go to Project Settings -> Plugins -> Install RoboPojoGenerator
This will generate the Java class for every Json Object. Ex below
public class Demo{
@SerializedName("offer")
@Expose
private Offer offer;
@SerializedName("id")
private String id;
@SerializedName("categories")
private Categories categories;
public void setOffer(Offer offer){
this.offer = offer;
}
public Offer getOffer(){
return offer;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return id;
}
public void setCategories(Categories categories){
this.categories = categories;
}
public Categories getCategories(){
return categories;
}
@Override
public String toString(){
return
"Offer{" +
"offer = '" + offer + '\'' +
",id = '" + id + '\'' +
",categories = '" + categories + '\'' +
"}";
}
}
@SerializedName and @Expose annotation.
This will solve your problem.
Upvotes: 1
Reputation: 1068
Use Iterator to get all dynamic key of your categories object.
private void parseCategoriesJson(JSONObject data) {
// here data is your categories object.
if (data != null) {
Iterator<String> it = data.keys();
while (it.hasNext()) {
String key = it.next();
try {
JSONObject object=data.getJSONObject(key);
// object is your electronics,food,fashion
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
Upvotes: 1