简体   繁体   English

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

[英]JSON with Jackson with unknown property name

I have the current issue that I receive a JSON response where I don't know the name of the key (in my example JSON named UnknownSubCategorieX ). 我有一个当前问题,我收到一个不知道密钥名称的JSON响应(在我的示例JSON中名为UnknownSubCategorieX )。

The keys are always different from each other. 键始终彼此不同。

Here a JSON example: 这是一个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"
    }
  ],
  ...
}

This function could be applied for the inner part 此功能可应用于内部

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
}

inner part: 内部:

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

class

@Data
public class OpenOrderPOJO {

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

}

With the suggestion of Mạnh Quyết Nguyễn 在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);
    }
}

and this execution 这个执行

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

I get this exception 我得到这个例外

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

Please help me to deserialize the whole JSON response. 请帮助我反序列化整个JSON响应。

It could be for example a map ´HashMap´ where the key is UnknownSubCategorie` 例如,地图“ HashMap” the key is UnknownSubCategorie。

However I tried to implement it, I got an exception and have meanwhile really a headache here. 但是我尝试实现它,但遇到了一个例外,同时在这里确实很头疼。

I am not fixed to JACKSON, if someone can only help with GSON I would join this :) 我不只限于杰克逊,如果有人只能为GSON提供帮助,我也可以加入:)

Look likes you want to map with your JSON where the key is unspecified (your example of duplicated key might confuse other people here). 看起来您想使用未指定密钥的JSON映射(您的重复密钥示例可能会使此处的其他人感到困惑)。

To dynamic matching, you can use @JsonAnySetter : 要进行动态匹配,可以使用@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);
    }
}

Now you can use it to deserialize with ObjectMapper as usual 现在您可以像往常一样使用它与ObjectMapper反序列化

Just add this in you POJO class and getters and setters for it 只需将其添加到您的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