如何使用GSON WHen对象解析JSON是JSONObject还是JSONArray [英] How Parse JSON using GSON WHen object is either JSONObject or JSONArray

查看:110
本文介绍了如何使用GSON WHen对象解析JSON是JSONObject还是JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gson将JSON转换为Java对象.我在json中有一个字段,我们的明智服务人员根据数据库中返回的项目数将它们编码为数组或对象.问题是如何对将Java对象传递到Gson转换器中的模型进行建模,以便我可以处理两种类型?

I am using Gson to convert JSON into a Java object. I have a field in the json that our wise services people coded as either an array or object depending on how many items come back in database. Question is how do model the Java object to pass into Gson converter so that I can handle both types?

json = new Gson().fromJson(reader, Order.class);

Java类只能正确解析JSON数组

Java Classes only parses the JSON array properly

public class Order {
    private Detail detail
}

public class Detail {
    public String id;
    public List<Type> types;
    //// getters and setters
}

public class Type {
    public String typeId;
    public String typeName
    //// getters and setters
}

JSON数据数组

{
    "detail":{
        "id":"1234565",
        "types":{
            "type":[
                {"typeId":"1246565","typeName":"MyName1"},
                {"typeId":"1444445","typeName":"MyName2"}
            ]
        }
    }
}

JSON数据对象

{
    "detail":{
        "id":"1234565",
        "types":{
            "type":{"typeId":"1246565","typeName":"MyName1"}
        }
    }
}

推荐答案

我想出了如何通过使用通用对象来做到这一点.因此,现在我仍然可以使用GSON进行调用,而不必在重构对象以及仅重构特定对象时需要做很多手动解析.

I figured out how to do this by using a generic object. So now I can still call using GSON and not have to do that much manual parsing only when I need when I reconstitute the Objects, and only that particular object.

json = new Gson().fromJson(reader, Order.class);

主要对象

public class Order {
    private Detail detail
}

详细信息类

public class Detail {
    public String id;
    public Types types;

    //// getters and setters
    public Types getTypes() {
        return types;
    }

    public void setTypes(Types types) {
        this.types = types;
    }
}

Types类包含一个名为type的通用对象,因此它可以存储一个List 或解析JSONObject的LinkedTreeMap.然后,您必须将其手动插入到新的Type对象中.

Types class that contains a generic Object called type so it can store either a List or a LinkedTreeMap which is how JSONObject gets parsed. You have to then manually insert it into a new Type object .

public class Types {
    private Object type;

    //// getters and setters

    public Object getType() {
        return type;
    }

    public void setType(Object type) {
        this.type = type;
    }

    public List<Type> getListType() {
        if (type instanceof List) {
            return (List<Type>) type;
        } else
            return null;
    }

    public Type getObjectType() {
        if (type instanceof Type) {
            return (Type) type;
        } else if (type instanceof Map) {
            Map<String, String> map = (Map<String, String>)type;
            Type newType = new Type((String)map.get("typeId"), (String)map.get("typeName"));
            return newType;
        }
        return null;
    }

public class Type {
    private String typeId;
    private String typeName;

    public Type() {}

    public Type(String id, String name) {
        this.typeId = id;
        this.typeName = name;
    }

    //// getters and setters
    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }
}

这篇关于如何使用GSON WHen对象解析JSON是JSONObject还是JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆