当前位置: 首页 > 知识库问答 >
问题:

无法从START_ARRAY标记反序列化java.util.LinkedHashMap实例

许阿苏
2023-03-14

下面提到的是JSON字符串ResultString:

{
"imageMaps": [{
        "crc": "c2c4",
        "flags": "0",
        "length": "117384",
        "index": 1,
        "version": "1.1.90ea",
        "status": ""
    }, {
        "crc": "7601",
        "flags": "8",
        "length": "117592",
        "index": 2,
        "version": "1.1.90ed",
        "status": ""
    }],
    "complete": true,
    "nextBootImageVersion": "",
    "lastKnownGoodImageVersion": "1.1.90ed",
    "runningImageVersion": "1.1.90ed"
}

我想将相同的内容转换为A类的对象:

public class A {

    private boolean complete;

    private String message;

    private String lastKnownGoodImageVersion;

    private String nextBootImageVersion;

    private String runningImageVersion;

    private Map<String, B> imageMaps;

    private  List<B> images;

    private MacID macId;

}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);    
A a = objectMapper.readValue(resultString, A.class);

B类代码为:

public static class B {
    public String version;
    public int flags; 
    public int crc; 
    public long length; 
    public String index;
    public String status;
}

但是获取异常:

JSONMappingException:无法从START_ARRAY标记反序列化java.util.LinkedHashMap实例

共有1个答案

柳羽
2023-03-14

在类中,您将属性ImageMaps声明为映射 ,但在JSON中ImageMaps是B的数组。如果在JSON中将ImageMaps更改为Images,则反序列化应该可以工作。

 类似资料: