【问题标题】:Read json string with array values读取带有数组值的 json 字符串
【发布时间】:2019-10-21 11:45:16
【问题描述】:

在使用对象映射器读取值方法读取 json 字符串时发现问题。可以检索各个字段但无法读取数组值,如下所述。

{
  "Stuname":"Test",
  "State":"CREATE",
  "resourceIds": ["{stuId: 814981025958, branches:[10, 4946]}"]
}

使用

objectMapper.readValue(message, StudentState.class);

其中 stuname、state 和 resourceids 集合以及数组也尝试过。但无法阅读。 对于 resourceids 创建了单独的类,其中包含 stuId 和位置数组。

学生状态班:

public class StudentState{
    private String Stuname;
    private String state;
    private Resources []  resourceIds;
    //private Collection<Resources> resourceIds;
    //Added respective getter/ setter
}

【问题讨论】:

  • 能否将StudentState 类的定义添加到问题中?
  • 你确定你的JSON? resourceIds 是一个没有字符串的数组?

标签: java json objectmapper


【解决方案1】:

您的 json 数组值看起来无效,它看起来像是一个嵌入了 json 的数组。大概应该是这样的:

{
  "Stuname": "Test",
  "State": "CREATE",
  "resourceIds": [
    {
      "stuId": 814981025958, "branches": [10, 4946]
    }
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2020-03-30
    • 1970-01-01
    • 2023-04-09
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多