如何在改造中调用动态嵌套json对象

时间:2019-09-09 09:09:45

标签: java android json kotlin retrofit2

如何使用Retrofit在android中调用动态嵌套json对象。我有以下格式的JSON结果,我的问题是:由于“食物”,“时尚”等是如何访问“类别”的内容所有动态值?给我一个主意。


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

    }
}
}

4 个答案:

答案 0 :(得分:1)

使用Pojo为您的json生成类。

要遵循的步骤

转到包->新建->选择从JSON生成POJO

如果未显示选件,则表明您尚未安装插件。

转到项目设置->插件->安装RoboPojoGenerator

这将为每个Json对象生成Java类。在下面

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和@Expose注释。

这将解决您的问题。

答案 1 :(得分:1)

使用Iterator获取类别对象的所有动态键。

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

答案 2 :(得分:1)

您可以使用HashMap

class Offer {
    private String id;
    private HashMap<String, Category> categories;

    // getters and setters
}

Category数据类应如下所示:

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

答案 3 :(得分:1)

这是json所需的模型列表,您的模型应如下所示:

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