我有JSON数据,看起来像这样
{ "会话ID":7242750700467747000}
该号码先前从服务器响应中获取,并在服务器端生成为Java Long.客户端认为自己认为这是sessionID并发送请求.问题是,当客户端的请求到达我不得不再次解析这个值long类型的服务器.我使用JsonPath,具体来说:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.8.1</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
当我像这样解析JSON数据时
long sessionID = JsonPath.read(json,"$ .sessionID");
我得到一个例外:
java.lang.ClassCastException:java.lang.Integer无法强制转换为java.lang.Long
因此看起来这个数字是由JsonPath解析为Integer的.这肯定会导致错误的结果,因为Integer小于Long.在JsonPath中有什么办法可以解析并将数据作为Long返回吗?
鉴于json等
{"arr1" : [1,2,3], "arr2" : []}
Run Code Online (Sandbox Code Playgroud)
在哪里知道有名为arr1和arr2的数组,是否有一个jsonpath表达式用于数组的长度,它将适用于空数组和非空数组?
我使用谷歌代码的JsonPath库做了几次尝试,注意:
JsonPath.read(json, "arr1.length")
Run Code Online (Sandbox Code Playgroud)
但它给出了一个错误,说数组没有属性.
我正在寻找解析为数组长度的单个字符串路径,而不是后续调用中间体,例如
JsonPath.read(json, "arr1").length // useless to me
Run Code Online (Sandbox Code Playgroud)
表达的任何复杂程度都是可以接受的.
对于上下文,我想提供一个带路径/值对的测试套件,这对于值很有用,但是我不能用这个模式断言数组大小.
我写了Spring控制器Junits。我使用JsonPath使用来从JSON获取所有ID ["$..id"]。
我有以下作为测试方法:
mockMvc.perform(get(baseURL + "/{Id}/info", ID).session(session))
.andExpect(status().isOk()) // Success
.andExpect(jsonPath("$..id").isArray()) // Success
.andExpect(jsonPath("$..id", Matchers.arrayContainingInAnyOrder(ar))) // Failed
.andExpect(jsonPath("$", Matchers.hasSize(ar.size()))); // Success
Run Code Online (Sandbox Code Playgroud)
以下是我传递的数据:-
List<String> ar = new ArrayList<String>();
ar.add("ID1");
ar.add("ID2");
ar.add("ID3");
ar.add("ID4");
ar.add("ID5");
Run Code Online (Sandbox Code Playgroud)
我收到以下失败消息:
Expected: [<[ID1,ID2,ID3,ID4,ID5]>] in any order
but: was a net.minidev.json.JSONArray (<["ID1","ID2","ID3","ID4","ID5"]>)
Run Code Online (Sandbox Code Playgroud)
问题是:如何使用JSONArray进行处理?org.hamcrest.Matchers;有没有使用JSONPath的简单方法。
设置: - ,,hamcrest-all-1.3 jarjson-path-0.9.0.jarspring-test-4.0.9.jar
我安装了 jsonpath-rw 1.4.0
sudo apt-get install python-jsonpath-rw
其中带有一个/usr/bin/jsonpath. 但这在使用 Filter 表达式时给了我错误:
$ jsonpath '$..book[?(@.price < 10)]' book.json
Traceback (most recent call last):
File "/usr/bin/jsonpath", line 11, in <module>
load_entry_point('jsonpath-rw==1.4.0', 'console_scripts', 'jsonpath.py')()
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/bin/jsonpath.py", line 71, in entry_point
main(*sys.argv)
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/bin/jsonpath.py", line 57, in main
expr = parse(args.expression)
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/parser.py", line 14, in parse
return JsonPathParser().parse(string)
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/parser.py", line 32, in parse
return self.parse_token_stream(lexer.tokenize(string))
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/parser.py", line 55, in parse_token_stream
return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
File "/usr/lib/python2.7/dist-packages/ply/yacc.py", line 331, in …Run Code Online (Sandbox Code Playgroud) 我有一个像bellow的json数组:
{
"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,
"likes": 1
},
{
"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
}
},
"expensive": 10}
Run Code Online (Sandbox Code Playgroud)
我试图找到没有 …
大多数示例都涉及Stefan G\xc3\xb6ssner 的书店示例,但是我正在努力为简单对象(无数组)定义正确的 JsonPath 表达式:
\n\n{ "Id": 1, "Name": "Test" }\nRun Code Online (Sandbox Code Playgroud)\n\n检查此 json 是否包含Id = 1.
我尝试了以下表达式:$..?[(@.Id == 1]),但这确实使用 Json.NET 找到了任何匹配项?
还尝试了Manatee.Json进行解析,似乎 jsonpath 表达式可能是这样的$[?($.Id == 1)]?
尽管名称看起来相似,但细微的更改可能会很棘手。不幸的是,我找不到有关 JPath 的体面信息。
JSON.NET 的文档正在谈论 JPath 和 JSONPath,我认为它们是相同的。我对么?
包含 JPath 表达式的字符串。
来自JToken.SelectToken(另见源代码)
此示例加载 JSON,然后使用 SelectToken(String) 和 JSONPath 查询从中查询值。
哪个正在使用JObject.SelectToken(继承自JToken)
在调用状态任务时,是否可以指定多个 InputPath,或者“选择”多个 JSON 节点以传递给任务的输入?
示例:
If this is the execution input:
{
"id":"identifier",
"nestedObjectA": {
"doubleNestedObjectA": {
"valueA": "value"
}
},
"nestedObjectB" : {
"valueB": "value"
}
}
是否可以将以下内容作为输入传递给一项任务:
{
"id":"identifier",
"nestedObjectB" : {
"valueB": "value"
}
}
和以下到其他?
{
"id":"identifier",
"nestedObjectA": {
"doubleNestedObjectA": {
"valueA": "value"
}
}
}
有一个返回一个非常基本的json响应的服务:
{
"methodresult": "error",
"ErrorCode": 2,
"ErrorCodeText": "format",
"ErrorMessage": "Json parse exception at pos:171 msg:Expecting \"key\""
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用JSONPath来查询"methodresult"值是否返回"错误".
基于我见过的文档/示例,我希望这可以工作:
$[?(@.methodresult=="error")]
Run Code Online (Sandbox Code Playgroud)
但是基于我正在使用的验证器(https://jsonpath.curiousconcept.com/)我没有看到任何布尔响应.
当试图用一个不在数组中的东西写一个表达式时,我有什么东西不见了?
这个可以换吗
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: elastic-operator
labels:
argocd.application.type: "system"
spec:
ignoreDifferences:
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jsonPointers:
- /webhooks/0/clientConfig/caBundle
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jsonPointers:
- /webhooks/1/clientConfig/caBundle
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jsonPointers:
- /webhooks/2/clientConfig/caBundle
Run Code Online (Sandbox Code Playgroud)
对于使用“通配符”的东西?以下是我正在寻找的“非工作”示例:
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: elastic-operator
labels:
argocd.application.type: "system"
spec:
ignoreDifferences:
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jsonPointers:
- /webhooks/[*]/clientConfig/caBundle
Run Code Online (Sandbox Code Playgroud)
我无法在他们的文档中找到任何说明这是可能的或相反的内容。iehttps://argoproj.github.io/argo-cd/user-guide/diffing/ 似乎正在使用“json-patch”,但阅读了一些 RFC,我也找不到有关通配符的任何信息。
谢谢!
jsonpath ×10
java ×3
json ×3
json.net ×2
argocd ×1
arrays ×1
expression ×1
filter ×1
groovy ×1
hamcrest ×1
jpath ×1
json-patch ×1
junit ×1
python ×1
python-2.7 ×1
size ×1
spock ×1
spring-test ×1
types ×1