json - com.jayway.jsonpath.JsonPath 找不到 key

标签 json

我正在尝试使用 com.jayway.jsonpath.JsonPath 从 JSON 字符串读取键/值:

String contentAsString = mvcResult.getResponse().getContentAsString();
System.out.println(contentAsString);
Object value = JsonPath.read(contentAsString, "$.key");

但我收到错误:

Expected to find an object with property ['key'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.

打印 contentAsString 给出:

[{"firstName":"N","key":"mykey"}]

contentAsString 不是有效的 JSON 吗?

最佳答案

您发布的 blob不是有效的 JSON 对象,但是,它是有效的 JSON 数组

要使用 JsonPath 阅读此内容,请尝试使用以下内容:

Object value = JsonPath.read(contentAsString, "$[0].key");

这将从初始数组的第 0 个元素获取 key 对象的值。

关于json - com.jayway.jsonpath.JsonPath 找不到 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40853263/

相关文章:

java - 为 JSON 响应启用 @XMLAttribute

javascript - 使用正则表达式删除引号内的引号

java - JSON对象到Java对象的映射是如何完成的?

php - 使用 JSON 绘制具有多条线的单个 Google 折线图

android - Android 中的 JSON 数组拆分器

javascript - 何时在 ajax 调用中使用 JSON.stringify()

Python 与 R 的 fromJSON()、unlist()、attr() 等价的函数?

json - hadoop + ambari 集群更改配置

javascript - 在 Node.js 中,如果多次请求,是否需要多次加载所需的模块?

python - 循环遍历蓝图中的所有规则并根据文件、flask 检查 json