0

有人问过类似的问题 如何编码嵌入在 JSON 中的 JSON

现在我传入的 json 看起来像

{
  "items": [
    {
      "context": {
        "rdw": {
          "queryId": "12345",
          "filterId": "54321"
        }
      },
      "startTimestamp": "2012-09-08T22:47:31-07:00",
      "endTimestamp": "2012-09-08T22:47:31-07:00",
      "mrn": "12345",
      "units": [
        "1",
        "2",
        "3"
      ],
      "types": [
        "1",
        "2",
        "3"
      ],
      "minDurationSeconds": "5"
    }
  ]
}

其中 Context 对象的一部分将是可变的,源系统将相应地发送因此对于 Context 字段,我们需要将整个 Json 作为

{
        "rdw": {
          "queryId": "12345",
          "filterId": "54321"
        }
}

作为字符串

对于以后的用例,它也需要被解析为一个对象。

添加使用 com.fasterxml.jackson.databind.ObjectMapper.ObjectMapper 进行解析的代码

WaveformQuery waveformQuery = new ObjectMapper().readValue(
                    waveformQueryStr, WaveformQuery.class);

这会引发错误

Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: java.io.StringReader@66e2cf6e; line: 4, column: 7] (through reference chain: WaveformQuery["items"]->Items["context"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
4

0 回答 0