简体   繁体   English

如何使用GSON WHen对象解析JSON是JSONObject还是JSONArray

[英]How Parse JSON using GSON WHen object is either JSONObject or JSONArray

I am using Gson to convert JSON into a Java object. 我正在使用Gson将JSON转换为Java对象。 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. 我在json中有一个字段,我们的明智服务人员根据数据库中返回的项目数将它们编码为数组或对象。 Question is how do model the Java object to pass into Gson converter so that I can handle both types? 问题是如何对传递给Gson转换器的Java对象建模,以便我可以处理两种类型?

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

Java Classes only parses the JSON array properly Java类只能正确解析JSON数组

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 data array JSON数据数组

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

JSON data object JSON资料物件

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

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. 因此,现在我仍然可以使用GSON进行调用,而不必仅在我需要重新构造对象以及仅重新构造特定对象时进行大量手动解析。

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

Main Object 主要对象

public class Order {
    private Detail detail
}

Detail Class 详细等级

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 class that contains a generic Object called type so it can store either a List or a LinkedTreeMap which is how JSONObject gets parsed. Types类包含一个名为type的通用对象,因此它可以存储List或LinkedTreeMap,这是解析JSONObject的方式。 You have to then manually insert it into a new Type object . 然后,您必须手动将其插入新的Type对象。

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

Sigh!! 叹!!

JsonArray typeArray; 
JsonElement typeElement = types.get("type"); 
if (typeElement.isJsonObject()) { 
    typeArray = new JsonArray();
    typeArray.add(typeElement); 
}
else {
    typeArray = (JsonArray)typeElement;
}
for (int i = 0; i < typeArray.size(); i++) {
    JsonObject typeObject = (JsonObject)(typeArray.get(i));
    typeObject.doSomethingWithThis();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM