单独使用单个JSONPath表达式,是否可以执行某种"OR"或"||" 运营商.例如,这两个JSONPath布尔表达式用于检查日志JSON文件的严重性:
$..log[?(@.severity == 'WARN')]
$..log[?(@.severity == 'Error')]
Run Code Online (Sandbox Code Playgroud)
但我想做一些逻辑上相似的事情:
$..log[?(@.severity == 'WARN' or @.severity == 'Error')] //this is not correct
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我有一段代码需要循环通过一个从REST服务的响应中获得的JSON数组.(这里有完整的要点.)
.exec(http("Request_1")
.post("/endPoint")
.headers(headers_1)
.body(StringBody("""REQUEST_BODY""")).asJSON
.check(jsonPath("$.result").is("SUCCESS"))
.check(jsonPath("$.data[*]").findAll.saveAs("pList")))
.exec(session => {
println(session)
session
})
.foreach("${pList}", "player"){
exec(session => {
val playerId = JsonPath.query("$.playerId", "${player}")
session.set("playerId", playerId)
})
.exec(http("Request_1")
.post("/endPoint")
.headers(headers_1)
.body(StringBody("""{"playerId":"${playerId}"}""")).asJSON
.check(jsonPath("$.result").is("SUCCESS")))
}
Run Code Online (Sandbox Code Playgroud)
第一个请求的响应格式是
{
"result": "SUCCESS",
"data": [
{
"playerId": 2
},
{
"playerId": 3
},
{
"playerId": 4
}
]
}
Run Code Online (Sandbox Code Playgroud)
并playerId在会话中显示为
pList -> Vector({playerId=2, score=200}, {playerId=3, score=200}
Run Code Online (Sandbox Code Playgroud)
我在第二次请求中看到了身体
{"playerId":"Right(empty iterator)}
Run Code Online (Sandbox Code Playgroud)
预计:3个身体要求
{"playerId":1}
{"playerId":2}
{"playerId":3}
Run Code Online (Sandbox Code Playgroud)
如果我只保存playerIds,我可以成功遍历结果数组:
.check(jsonPath("$.data[*].playerId").findAll.saveAs("pList")))
Run Code Online (Sandbox Code Playgroud) 如果我使用了不正确的术语,请原谅我,我是新手。
我有一些简单的 JSON:
{
"properties": {
"footer.navigationLinks": {
"group": "layout"
, "default": [
{
"text": "Link a"
, "href": "#"
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图查明“footer.navigationLinks”,但我在键名中遇到点问题。我正在使用http://jsonpath.com/并且当我输入时
$.properties['footer.navigationLinks']
Run Code Online (Sandbox Code Playgroud)
我得到“不匹配”。如果我将键更改为“footernavigationLinks”,它会起作用,但我无法控制 JSON 文件中的键名。
请有人帮我定位那个键名吗?
我有这个JSON:
{
"client_id": "26075235",
"client_version": "1.0.0",
"event": "app.uninstall",
"timestamp": 1478741247,
"data": {
"user_id": "62581379",
"site_id": "837771289247593785",
"platform_app_id": "26075235"
}
}
Run Code Online (Sandbox Code Playgroud)
我将它解析为JSON.NET JObject,我可以使用例如(字符串)RequestBody.SelectToken("client_id")成功访问第一级值
如何使用JPath表达式(或通过访问JSON.NET JObject的子对象)访问"user_id"的值?这不起作用:
(string)RequestBody.SelectToken("data[0].user_id")
Run Code Online (Sandbox Code Playgroud)
我不能这样做来解析JSON的'data'部分:
JObject RequestBodyData = JObject.Parse((string)RequestBody.SelectToken("data"));
Run Code Online (Sandbox Code Playgroud)
因为编译器似乎将RequestBody.SelectToken("data")识别为对象(我得到错误'无法将对象解析为字符串')
我不想将原始JSON解析为自定义C#对象,因为我正在开发一个解决方案,需要能够将JSON一般性地解析为JObject(或任何其他类型的通用对象来处理JSON),所以它可以以相对一致的方式解析.
嘿所有,我想知道是否有人知道使用正则表达式或通配符运算符(或'%LIKE%'SQL中的pehaps )的方法,所以我可以使用JSONPath在一大组JSON数据中进行搜索.
例如(是的,我正在解析,而不是eval( )我在应用程序中的数据):
var obj = eval ( '({ "hey": "can you find me?" })' );
Run Code Online (Sandbox Code Playgroud)
我希望能够像这样查看数据:
$.[?(@.hey:contains(find))] // (in jQuery terminology)
Run Code Online (Sandbox Code Playgroud)
其中参数的内容是{ "key" : "value" }我数据中对中的部分或全部值.
目前,我只找到文件上>,<,=,和!=关系运算符,它不给我那么多的灵活性.
有谁知道一个方法可以让我只是刚刚 JSONPath找到这个数据(不通过的所有条目具有循环)?
我不想使用Dojo的JSONQuery,因为这需要另一个库.但是,它允许你这样做,这里是他们的例子:
[?description~‘*the*’]
Run Code Online (Sandbox Code Playgroud)
问我是否想要更多澄清这个问题.
我正在使用MockMvc进行一些测试,我想验证JSON响应的结构.具体来说,我想确保属性的键存在,并且该值是某个类型或null.
{
"keyToNull": null, # This may be null, or a String
"keyToString": "some value"
}
Run Code Online (Sandbox Code Playgroud)
以下对我有用,但我想知道是否有办法将每组两个期望合并为一行,因为我有很多要检查的属性:
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.hamcrest.Matchers.*;
.andExpect(jsonPath("$").value(hasKey("keyToNull")))
.andExpect(jsonPath("$.keyToNull").value(
anyOf(any(String.class), nullValue(String.class))))
.andExpect(jsonPath("$").value(hasKey("keyToString")))
.andExpect(jsonPath("$.keyToString").value(
anyOf(any(String.class), nullValue(String.class))))
Run Code Online (Sandbox Code Playgroud)
这hasKey()是必要的,因为另一个断言本身通过,因为MockMvc的实现映射中不存在的键映射为null:
.andExpect(jsonPath("$.notAKey").value(
anyOf(any(String.class), nullValue(String.class)))) // ok
Run Code Online (Sandbox Code Playgroud)
jsonPath().exists()也不起作用,因为在内部它比较了价值null.
我考虑过这样一个单独的方法:
private static <T> void assertNullableAttr(ResultActions res, String jsonPath, Class<T> type) throws Exception {
int split = jsonPath.lastIndexOf('.');
String prefix = jsonPath.substring(0, split), key = jsonPath.substring(split+1);
res.andExpect(jsonPath(prefix).value(hasKey(key)))
.andExpect(jsonPath(jsonPath).value(anyOf(any(type), nullValue(type))));
}
Run Code Online (Sandbox Code Playgroud)
但后来它迫使我以不自然的方式分割我的代码:
ResultActions res = mockMvc.perform(get("/api"))
// these …Run Code Online (Sandbox Code Playgroud) 我正在使用一个返回带有不规则对象数组的结构的JSON提要 - 我无法预测数组中的内容,并且随着时间的推移它会发生什么变化.一个简单的例子是
{
"content":
[
{
"person" : "john"
},
{
"car" : "Audi"
}
]
}
Run Code Online (Sandbox Code Playgroud)
JsonPath查询
$.content.[0]
Run Code Online (Sandbox Code Playgroud)
返回该数组中的第一个对象.但是,我需要一个jsonpath查询返回该对象的第一个属性的文字NAME,在这种情况下我需要查询返回"person".这可能吗?
所以我正在研究下面的json:
{
"id": "",
"owner": "some dude",
"metaData": {
"request": {
"ref": null,
"contacts":[
{
"email": null,
"name": null,
"contactType": "R"
},
{
"email": null,
"name": "Dante",
"contactType": "S"
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想检索name联系人的类型S,只检索返回的第一个.
在此路径中使用jsonpath "$..contacts[?(@.contactType == 'S')].name"始终返回一个字符串数组,因为过滤器操作始终将结果作为数组返回.
所以,我想"$..contacts[?(@.contactType == 'S')].name[0]"和"$..contacts[?(@.contactType == 'S')][0].name",但没有运气.那些路径返回空结果.
所以我的问题是,在jsonpath中使用过滤器时,有没有办法获得第一个元素.我目前正在使用jayway jsonpath v2.2.0.
考虑这个示例 JSON:
{
"thing": [
{
"name": "foo",
"flag": "yep"
},
{
"name": "bar"
},
{
"name": "baz",
"flag": "nope"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果我想找到所有具有相应“标志”的“名称”元素,我可以使用如下内容:
$.thing[?(@.flag)].name
Run Code Online (Sandbox Code Playgroud)
我会得到结果:
'0' => "foo"
'1' => "baz"
Run Code Online (Sandbox Code Playgroud)
但是,如果我想找到所有没有相应“标志”的“名称”元素怎么办?
(就这个问题而言,我不关心“标志”的值,只关心它是否存在)