Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过改造解析具有未知密钥的json?_Android_Json_Parsing_Retrofit - Fatal编程技术网

Android 如何通过改造解析具有未知密钥的json?

Android 如何通过改造解析具有未知密钥的json?,android,json,parsing,retrofit,Android,Json,Parsing,Retrofit,我想通过改造来解析这个JSON(我不知道字段名{xxx,yyy,zzz}) 我试了一些地图,但我做错了什么。 请帮我解析这个该死的json { xxx: [ { name: "name1", description: "desc1" } ], yyy: [ { name: "name2", description: "desc2"

我想通过改造来解析这个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;
}
公共类DishesCategoryResponse{
公共地图设置SMAP;
}
然后我尝试了这个,它成功了

@GET("/api/restaurant_menu/{id}")
    Observable<Map<String, List<Dish>>> getDishesCategory(@Path("id") long id);
@GET(“/api/restaurant\u menu/{id}”)
可观察的getDishesCategory(@Path(“id”)long id);

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

我有一个json文件:

final JSONObject bodyObject=新的JSONObject(body);
最终JSONObject活动=bodyObject.getJSONObject(“活动”);
最终迭代器键=activities.keys();//您可以遍历所有键
最终列表contactActivityInfoList=new ArrayList();
while(keys.hasNext()){
最后一个字符串键=keys.next();
最后一个字符串jsonString=activities.getJSONObject(key.toString();
最后联系活动联系活动=
fromJson(jsonString,ContactActivity.class);
contactActivityInfoList.add(contactActivity);
}

使用GsonFormat()等工具,这是映射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;
    }
}
}
public类YourClassName{
/**
*姓名:姓名1
*描述:描述1
*/
私人名单xxx;
/**
*姓名:姓名2
*描述:描述2
*/
私人名单yyy;
/**
*姓名:姓名3
*描述:desc3
*/
私人名单zzz;
公共无效集合xxx(列表xxx){
这是xxx=xxx;
}
公共void setYyy(列表yyy){
this.yyy=yyy;
}
公共无效设置zzz(列表zzz){
this.zzz=zzz;
}
公共列表getXxx(){
返回xxx;
}
公共列表getYyy(){
返回yyy;
}
公共列表getZzz(){
返回zzz;
}
公共静态类XxxEntity{
私有字符串名称;
私有字符串描述;
公共void集合名(字符串名){
this.name=名称;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共字符串getName(){
返回名称;
}
公共字符串getDescription(){
返回说明;
}
}
公共静态类实体{
私有字符串名称;
私有字符串描述;
公共void集合名(字符串名){
this.name=名称;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共字符串getName(){
返回名称;
}
公共字符串getDescription(){
返回说明;
}
}
公共静态类{
私有字符串名称;
私有字符串描述;
公共void集合名(字符串名){
this.name=名称;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共字符串getName(){
返回名称;
}
公共字符串getDescription(){
返回说明;
}
}
}

谢谢你的插件,但在这种情况下,xxx,yyy,zzz是dinamic值,这个解决方案没有帮助OP在问题“我不知道字段名”中说,我将“我不知道字段名{xxx,yyy,zzz}”解释为“一些我不知道的固定值”。@njzk2为什么?获取字符串响应并重新分析it@njzk2Gson包中带有JsonArray.java的自定义反序列化程序也有同样的功能。如果没有自定义的反序列化程序,你就无法很好地管理它。我认为,只需创建映射并遍历键在大多数情况下,改型使用对象映射器(通常是gson),因此你不必自己处理响应的实际主体。是的,自定义反序列化程序可以执行您在此处建议的操作。@njzk2在gson的JsonElement中没有迭代键的方法
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;
    }
}
}