简体   繁体   English

异构JSON数组未与Jackson解析

[英]Heterogeneous JSON array not parsing with jackson

I have a json structure I am trying to parse with jackson which at the top level consists of a heterogenous array: 我有一个json结构,我想与杰克逊解析,杰克逊在顶层由一个异构数组组成:

[
  {
    "machineuuid": "e74f75b9-d819-4377-b5cd-8bc0bd02dee2",
    "version": 5,
    "flags": 10
  },
  [
    {
      "ma": [
        {
          "arr": [
            3,
            7,
            0
          ],
          "ts": "2014-12-02T18:56:50.514Z",
          "u": "74e4d525-ae1a-5406-8b4b-0166d03bf2a4"
        }
      ],
      "u": "46e800d6-2e8c-469c-9d39-9f985bf19728",
      "web": [
        {
          "arr": [
            3,
            1,
            0
          ],
          "cc": [
            "7a26ff80-6a42-4648-bdbc-0076e7e31088"
          ],
          "p": 100,
          "t": "Disney.com | The official home for all things Disney",
          "ts": "2014-12-02T18:56:55.173Z",
          "u": "http://disney.com/"
        }
      ]
    }
  ]
]

The top level java looks like: 顶级的Java看起来像:

public class ActivityUpload {

    @JsonProperty(index = 0)
    MachineInfo machineInfo;

    @JsonProperty(index = 1)
    List<UserUpload> userUploads;

}

The inner portions of the parsing work fine with unit tests (eg MachineInfo, UserUpload), it is just the top level where the outer array contains a MachineInfo json object in array[0], and the array of UserUpload json objects is in array[1]. 解析的内部部分可以很好地进行单元测试(例如MachineInfo,UserUpload),它只是顶层,外部数组在array [0]中包含MachineInfo json对象,而UserUpload json对象的数组在array [ 1]。

The exception that is thrown is: 引发的异常是:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.myapp.domain.ActivityUpload out of START_ARRAY token
 at [Source: [
  {
    "machineuuid": "e74f75b9-d819-4377-b5cd-8bc0bd02dee2",
    "version": 5,
    "flags": 10
  },
  [
    {
      "ma": [
        {
          "arr": [
            3,
            7,
            0
          ],
          "ts": "2014-12-02T18:56:50.514Z",
          "u": "74e4d525-ae1a-5406-8b4b-0166d03bf2a4"

.... lines omitted 

]; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:749)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:745)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1203)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:147)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)
    at com.myapp.domain.ActivityUploadTest.testPayload1(ActivityUploadTest.java:95)

I am guessing the problem might be my use of the JsonProperty(index=0) annotation, which I see was added in 2.4.0 (I am currently using 2.4.4). 我猜测问题可能出在我对JsonProperty(index = 0)注释的使用上,我看到它是在2.4.0中添加的(我目前正在使用2.4.4)。 I read the docs as the index parameter being able to select a particular position in an array. 我将文档作为index参数阅读,可以选择数组中的特定位置。 I would prefer to have a simply annotated class/es that I can give to jackson to parse rather than having to hand craft the parsing of the json directly with lower level methods (eg objectMapper.readTree()). 我宁愿有一个可以给杰克逊解析的简单注释的类,而不必直接用较低层的方法(例如objectMapper.readTree())手工进行json的解析。

I have no control over the format of the json that is sent to me to change in any way. 我无法控制发送给我以任何方式更改的json格式。

I have looked at many other posts, but they seem to deal with homogenous arrays, with all the same type of data within that array. 我看过许多其他文章,但它们似乎处理同质数组,并且该数组中的所有数据类型相同。 I deal with that in many places and those work as expected. 我在许多地方都处理过这些,并且这些工作都按预期进行。 I am hoping this is a simple oversight on my part. 我希望这只是我的简单疏忽。

Actually, there is a way to handle this by specifying "serialize-as-array" 实际上,有一种方法可以通过指定“ serialize-as-array”来处理

@JsonPropertyOrder({ machineInfo, userUploads })
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
public class ActivityUpload {
  @JsonProperty
  MachineInfo machineInfo;

  @JsonProperty
  List<UserUpload> userUploads;

}

and it should then match what you have. 然后它应该与您所拥有的匹配。

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

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