【问题标题】:Jackson Json not reading my json correctlyJackson Json 没有正确读取我的 json
【发布时间】:2014-11-06 16:40:52
【问题描述】:

我使用POST 方法将一些json 作为byte[] 发送到我的playframework 网站。

这是一个json的例子

{
   "productFamily": "abcd",
   "currentVersion": "12d11e3",
   "serialNumber": "asdfasfdasdf",
   "modelNumber": "sdfsfdasf",
   "productCode": "safasdfsdfasdfsdfsd"
}

我正在尝试使用此代码将其加载到JsonNode

public static Result submit() {
    JsonNode recievedJson = request().body().asJson();

Eclipse 调试说recievedJson

_value = "{\r\n "productFamily": "abcd",\r\n "currentVersion": "12d11e3",\r\n "serialNumber": "asdfasfdasdf",\r\n "modelNumber": "sdfsfdasf",\r\n "productCode": "safasdfsdfasdfsdfsd"}"

为什么我的 json 字符串没有从请求正文解析到 JsonNode obj 中?

当我尝试在 recievedJson 上调用 JsonNode 方法时,我没有得到预期的结果。例如recievedJson.get(0) 返回nullrecievedJson.fields() 返回null

对象项 = recievedJson.get("productFamily"); Eclipse 调试显示 item = null。

对象项 = recievedJson.fields(); 返回一个空的迭代器

【问题讨论】:

  • 什么? Eclipse 调试显示对象的表示。它有什么问题?
  • 当我尝试在 recievedJson 上调用 JsonNode 方法时,我没有得到预期的结果。例如 recievedJson.get(0) 返回 null 而 recievedJson.fields() 返回 null
  • JsonNode#get(int) 是做什么的?请告诉我们JsonNode#fields() 在您的情况下返回null。提供一个可重现的例子。
  • 例如 get() 应该返回一个数组节点的项目,或者如果你使用一个字符串你应该得到该字段的值。
  • 对,你没有数组节点,你有一个对象节点。

标签: java json playframework playframework-2.2


【解决方案1】:

您正在尝试使用 get(int) 方法访问数组节点,而您的 json 是一个对象。尝试改用get(String) 方法,其中String 是您的键名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 2014-02-27
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2015-06-12
    • 2023-02-13
    • 2019-09-04
    相关资源
    最近更新 更多