繁体   English   中英

具有未知属性名称的Jackson的JSON

[英]JSON with Jackson with unknown property name

我有一个当前问题,我收到一个不知道密钥名称的JSON响应(在我的示例JSON中名为UnknownSubCategorieX )。

键始终彼此不同。

这是一个JSON示例:

{
  "UnknownSubCategorie1": [],
  "UnknownSubCategorie2": [
    {
      "orderNumber": "120466",
      "type": "sell",
      "name": "ProductA"
    },
    {
      "orderNumber": "120467",
      "type": "sell",
      "name": "ProductB"
    }
  ],
  "UnknownSubCategorie3": [
    {
      "orderNumber": "120345",
      "type": "sell",
      "name": "ProductA"
    },
    {
      "orderNumber": "134006",
      "type": "sell",
      "name": "ProductB"
    },
    {
      "orderNumber": "134003",
      "type": "sell",
      "name": "ProductB"
    }
  ],
  ...
}

此功能可应用于内部

ObjectMapper mapper = new ObjectMapper();
List<OpenOrderPOJO> openOrderPOJOList = null;
try {
    openOrderPOJOList = mapper.readValue(response, mapper.getTypeFactory().constructCollectionType(List.class, OpenOrderPOJO.class));
} catch (IOException e) {
    e.printStackTrace();
}
if (openOrderPOJOList != null) {
    ...continue
}

内部:

   {
      "orderNumber": "120345",
      "type": "sell",
      "name": "ProductA"
    },
    {
      "orderNumber": "134006",
      "type": "sell",
      "name": "ProductB"
    },
    {
      "orderNumber": "134003",
      "type": "sell",
      "name": "ProductB"
    }

@Data
public class OpenOrderPOJO {

    private String orderNumber;
    private String type;
    private String name;

}

在MạnhQuyếtNguyễn的建议下

@Data
public class POJO {
    private Map<String, List<OpenOrderPOJO>> unknownSubCategories;

    @JsonAnySetter
    public void setMap(String key, List<OpenOrderPOJO> value) {
        if (unknownSubCategories == null) {
            unknownSubCategories = new LinkedHashMap<>();
        }
        unknownSubCategories.put(key, value);
    }
}

这个执行

public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();
        List<POJO> openOrderPOJOList = null;
        try {
            openOrderPOJOList = mapper.readValue(JSONResponse, mapper.getTypeFactory().constructCollectionType(List.class, POJO.class));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (openOrderPOJOList != null) {
            //continue
        }
    }

我得到这个例外

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
 at [Source: (StringReader); line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1138)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1092)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3042)
    at Poloniex.PrivateMethods.Main.main(Main.java:30)

Process finished with exit code 0

请帮助我反序列化整个JSON响应。

例如,地图“ HashMap” the key is UnknownSubCategorie。

但是我尝试实现它,但遇到了一个例外,同时在这里确实很头疼。

我不只限于杰克逊,如果有人只能为GSON提供帮助,我也可以加入:)

看起来您想使用未指定密钥的JSON映射(您的重复密钥示例可能会使此处的其他人感到困惑)。

要进行动态匹配,可以使用@JsonAnySetter

@Data
public class POJO {
    private Map<String, List<OpenOrderPOJO>> unknownSubCategories;

    @JsonAnySetter
    public void setMap(String key, List<OpenOrderPOJO> value) {
        if (unknownSubCategories == null) {
            unknownSubCategories = new LinkedHashMap<>();
        }
        unknownSubCategories.put(key, value);
    }
}

现在您可以像往常一样使用它与ObjectMapper反序列化

只需将其添加到您的POJO类中,并为其获取和设置

@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

暂无
暂无

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

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