Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
Java 当对象是JSONObject或JSONArray时,如何使用GSON解析JSON_Java_Json_Gson - Fatal编程技术网

Java 当对象是JSONObject或JSONArray时,如何使用GSON解析JSON

Java 当对象是JSONObject或JSONArray时,如何使用GSON解析JSON,java,json,gson,Java,Json,Gson,我正在使用Gson将JSON转换为Java对象。我在json中有一个字段,我们的智者服务人员根据数据库中返回的项目数将其编码为数组或对象。问题是如何建模Java对象以传递到Gson转换器中,以便处理这两种类型 json = new Gson().fromJson(reader, Order.class); Java类只正确解析JSON数组 public class Order { private Detail detail } public class Detail { pu

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

json = new Gson().fromJson(reader, Order.class);
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数据对象

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

JsonArray类型数组;
JsonElement typeElement=types.get(“type”);
如果(typeElement.isJsonObject()){
typeArray=新的JsonArray();
typeArray.add(typeElement);
}
否则{
typeArray=(JsonArray)typeElement;
}
对于(int i=0;i
我通过使用泛型对象找到了实现方法。因此,现在我仍然可以使用GSON进行调用,而不必仅在需要时进行大量的手动解析,当我重新构建对象时,并且只需要那个特定的对象

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;
    }
}
类型类,该类包含名为type的泛型对象,因此它可以存储列表或 或者是一个LinkedTreeMap,它是解析JSONObject的方式。然后必须手动将其插入到新类型对象中

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;
    }
}
公共类类型{
私有对象类型;
////接球手和接球手
公共对象getType(){
返回类型;
}
公共void集合类型(对象类型){
this.type=type;
}
公共列表getListType(){
如果(输入instanceof List){
返回(列表)类型;
}否则
返回null;
}
公共类型getObjectType(){
if(type instanceof type){
返回(类型)类型;
}else if(键入instanceof Map){
Map=(Map)类型;
Type newType=newType((String)map.get(“typeId”),(String)map.get(“typeName”);
返回新类型;
}
返回null;
}
公共类类型{
私有字符串类型ID;
私有字符串类型名;
公共类型(){}
公共类型(字符串id、字符串名称){
this.typeId=id;
this.typeName=名称;
}
////接球手和接球手
公共字符串getTypeId(){
返回typeId;
}
public void setTypeId(字符串typeId){
this.typeId=typeId;
}
公共字符串getTypeName(){
返回typeName;
}
public void setTypeName(字符串typeName){
this.typeName=typeName;
}
}

在GSON中,难道没有办法询问元素的类型吗?正如你所说,你的服务设计很差。你能和数据服务人员谈谈,只使用第一种类型(JSON数据数组)吗即使数据库中只有一个项目?这不仅会让您更容易,而且会让将来使用该服务的其他开发人员/团队更容易。例如,
isJsonArray
isJsonObject
(我在30秒内找到了它们,我甚至不使用GSON。)我别无选择,服务已经设置好,而且由于遗留问题,我不得不保持这种方式……我只是尝试不再手工解析对象,而是使用Gson自动解析。你确切的问题是什么?你想把JSON字符串都转换成java对象,而且事先还不知道。我不想分离JSON,因为我需要将其堆积到J中ava对象。我拥有的Java类比我现在展示的要复杂得多,有多个级别的Java对象。因此,我想通过Gson().fromJson()来实现它。我想知道是否有某种方法可以通过注释或其他方法在Java对象中实现它class@JPM-好吧,把它放回一起,然后按你想要的方式处理。
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;
    }
}
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;
    }
}