json中的json我需要考虑作为字符串字段

时间:2014-05-07 21:26:36

标签: java json gson fasterxml

有人问了类似的问题 How to encode JSON embedded within 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)

0 个答案:

没有答案