我在gatling中有需要提取特定JSON值的场景。例如:我想提取书名“世纪的谚语”的作者。我正在使用以下Jsonpath $..book[?(@.title eq 'Sayings of the Century')]
。但它没有给我任何回报。你能告诉我如何实现这个目标。
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
答案 0 :(得分:1)
正确的语法是==
,而不是eq
答案 1 :(得分:0)
java和scala不同。您应该在scala中清楚显示jsonPath的类型。
例如,在Java中,代码为:
JsonPath.read(jsonString, "$.message_metadata.event_type");
但是在scala中是:
JsonPath.read\[String\](jsonString, "$.message_metadata.event_type")
如果您的结果类型为JSONArray,则代码如下:
JsonPath.read\[JSONArray\](jsonString, "$.message_metadata.event_type")
希望这可以为您提供帮助。