如何通过Retrofit解析带有未知密钥的json?

时间:2016-04-20 11:31:06

标签: android json parsing retrofit

我想通过Retrofit解析这个JSON(我不知道字段名称{xxx,yyy,zzz}) 我尝试了一些地图,但我做错了。 请帮我解析这个该死的json

    {
        xxx: [
        {
        name: "name1",
        description: "desc1"
        }
        ],
        yyy: [
        {
        name: "name2",
        description: "desc2"
        }
        ],
        zzz: [
        {
        name: "name3",
        description: "desc3"
        },
{
        name: "name4",
        description: "desc4"
        }
        ]
        }

--- ---解决方案

我尝试创建类响应,但方法错误

public class DishesCategoryResponse {
    public Map<String, List<Dish>> settingsMap;
}

然后我尝试了这个并且它有效

@GET("/api/restaurant_menu/{id}")
    Observable<Map<String, List<Dish>>> getDishesCategory(@Path("id") long id);

2 个答案:

答案 0 :(得分:0)

有一天我有类似的任务。不确定我的解决方案是否完美,但它可能对您有帮助。

我有这个json文件:

Json screenshot

final JSONObject bodyObject = new JSONObject(body);
final JSONObject activities = bodyObject.getJSONObject("activities");
final Iterator<String> keys = activities.keys(); // you can iterate through all keys

final List<ContactActivity> contactActivityInfoList = new ArrayList<ContactActivity>();

while (keys.hasNext()) {
    final String key = keys.next();

    final String jsonString = activities.getJSONObject(key).toString();
    final ContactActivity contactActivity =
                mGson.fromJson(jsonString, ContactActivity.class);

    contactActivityInfoList.add(contactActivity);
}

答案 1 :(得分:-2)

使用像GsonFormat(Android studio plugin)这样的工具,这是映射你的json的类:

public class YourClassName {

/**
 * name : name1
 * description : desc1
 */

private List<XxxEntity> xxx;
/**
 * name : name2
 * description : desc2
 */

private List<YyyEntity> yyy;
/**
 * name : name3
 * description : desc3
 */

private List<ZzzEntity> zzz;

public void setXxx(List<XxxEntity> xxx) {
    this.xxx = xxx;
}

public void setYyy(List<YyyEntity> yyy) {
    this.yyy = yyy;
}

public void setZzz(List<ZzzEntity> zzz) {
    this.zzz = zzz;
}

public List<XxxEntity> getXxx() {
    return xxx;
}

public List<YyyEntity> getYyy() {
    return yyy;
}

public List<ZzzEntity> getZzz() {
    return zzz;
}

public static class XxxEntity {
    private String name;
    private String description;

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}

public static class YyyEntity {
    private String name;
    private String description;

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}

public static class ZzzEntity {
    private String name;
    private String description;

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}
}