com.jayway.jsonpath.JsonPath找不到密钥

时间:2016-11-28 20:53:10

标签: 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吗?

1 个答案:

答案 0 :(得分:2)

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

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

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

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

相关问题