Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 反序列化列表错误<;对象>;与格森_Java_Json - Fatal编程技术网

Java 反序列化列表错误<;对象>;与格森

Java 反序列化列表错误<;对象>;与格森,java,json,Java,Json,我调试CUSTOMER对象,其中包含列表: 这是CUSTOMER.class: public class CUSTOMER { @XmlElements({ @XmlElement(name = "ADDRESS", type = ADDRESS.class), @XmlElement(name = "ZIP", type = ZIP.class), @XmlElement(name =

我调试
CUSTOMER
对象,其中包含
列表

这是CUSTOMER.class:

   public class CUSTOMER { 
    
 @XmlElements({
        @XmlElement(name = "ADDRESS", type = ADDRESS.class),
        @XmlElement(name = "ZIP", type = ZIP.class),
        @XmlElement(name = "CITY", type = CITY.class),
        @XmlElement(name = "COUNTRY", type = COUNTRY.class),
    })
    protected List<Object> addressAndZIPAndCITY;
    
    // other fields
    }
缺少地址、邮政编码、城市和国家对象标题。 所以反序列化很糟糕

我无法更改
列表
声明。 是否有选项将其反序列化为带有地址、ZIP、城市等的json。?像这样:

{
    "addressAndZIPAndCITY": [{
            "ADDRESS": {
                "value": "some value",
                "type": "some type"
            }
        }, {
            "ZIP": {
                "value": "some value 2",
                "type": "some type 2"
            }
        }
    ]
}

编辑:我的意思是,GSON似乎不知道存在哪个对象,但您可能知道如何向模型中添加一些注释或其他内容来识别它?

我认为您必须为客户编写自定义序列化程序,或者创建另一个类来执行序列化:

public class GsonCustomer {
    private List<Map<String, Object>> addressAndZipAndCity = new ArrayList<>();
    // other fields
    public GsonCustomer(CUSTOMER customer) {
        for (Object o: customer.getAddressAndZipAndCity()) {
            Map<String, Object> map = new HashMap<>();
            map.put(o.getClass().getSimpleName(), o);
            addressAndZipAndCity.add(map);
        }
        // copy other fields
    }
}

// so you can do: gson.toJson(new GsonCustomer(customer))
公共类GsonCustomer{
私有列表地址和ZipandCity=新的ArrayList();
//其他领域
公共GsonCustomer(客户){
对于(对象o:customer.getAddressAndZipAndCity()){
Map Map=newhashmap();
put(o.getClass().getSimpleName(),o);
地址和ZipandCity.add(地图);
}
//复制其他字段
}
}
//所以您可以这样做:gson.toJson(新的GsonCustomer(customer))
或如前所述:

public class CustomerAdapter implements JsonSerializer<CUSTOMER> {

    @Override
    public JsonElement serialize(CUSTOMER customer, Type type, JsonSerializationContext jsonSerializationContext) {
        // TODO
    }
}

// Gson gson = new GsonBuilder().registerTypeAdapter(CustomerAdapter.class, new CustomerAdapter()).create();
公共类CustomerAdapter实现JsonSerializer{
@凌驾
公共JsonElement序列化(客户、类型、JsonSerializationContext JsonSerializationContext){
//待办事项
}
}
//Gson Gson=new GsonBuilder().registerTypeAdapter(CustomerAdapter.class,new CustomerAdapter()).create();

能否用自定义类替换列表,例如MyClass扩展ArrayList{}。然后您可以为它编写序列化程序和反序列化程序。顺便说一句,gson的行为是正确的。我预计会有这样的产出。您想要的输出与列表对应。我无法替换它。我能用另一种方法吗?
public class CustomerAdapter implements JsonSerializer<CUSTOMER> {

    @Override
    public JsonElement serialize(CUSTOMER customer, Type type, JsonSerializationContext jsonSerializationContext) {
        // TODO
    }
}

// Gson gson = new GsonBuilder().registerTypeAdapter(CustomerAdapter.class, new CustomerAdapter()).create();