0

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"
        },
        .........
        .........

    }
}
}
3
  • 2
    Please post the code you have tried. Commented Sep 9, 2019 at 9:10
  • just give me an idea to start Commented Sep 9, 2019 at 9:11
  • ideally the categories should be a json array and the inner object needs to have something like category_name: xyz ..... This will make the model easy and more dynamic to model Commented Sep 9, 2019 at 9:26

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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();
            }
        }
    }
}

Comments

1

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.

1 Comment

i believe you should also give him the pojo for the Categories object
1

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

1 Comment

are they limited at least? if yes you should add all models for each object else you can use HashMap<String, JSONObject>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.