我有一些JSON,其中以下是一个小样本:
{
"results": {
"div": [
{
"class": "sylEntry",
"div": [
{
"class": "sT",
"id": "sOT",
"p": "Mon 11/17, Computer work time"
},
{
"class": "des",
"id": "dOne",
"p": "All classes Siebel 0218"
}
],
"id": "sylOne"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我想只"p"用class 检索div元素的内容"sT".我想使用循环并执行以下操作:
var arrayOfResults = $.results..div.p
Run Code Online (Sandbox Code Playgroud)
不起作用,因为我只想用class检索div元素的p值"sT".
那么我该如何构造我的JSONpath以便它将检索divs类"sT"中包含的p元素数组.
谢谢!!
背景:
我在 JSON 中有以下示例数据结构:
{'sensor' : [
{'assertions_enabled': 'ucr+',
'deassertions_enabled': 'ucr+',
'entity_id': '7.0',
'lower_critical': 'na',
'lower_non_critical': 'na',
'lower_non_recoverable': 'na',
'reading_type': 'analog',
'sensor_id': 'SR5680 TEMP (0x5d)',
'sensor_reading': {'confidence_interval': '0.500',
'units': 'degrees C',
'value': '42'},
'sensor_type': 'Temperature',
'status': 'ok',
'upper_critical': '59.000',
'upper_non_critical': 'na',
'upper_non_recoverable': 'na'}
]}
Run Code Online (Sandbox Code Playgroud)
传感器列表实际上将包含许多包含传感器信息的字典。
问题:
我正在尝试使用 jsonpath 查询列表以返回传感器字典的子集,sensor_type=='Temperature'但我得到了'False'返回(不匹配)。这是我的 jsonpath 表达式:
results = jsonpath.jsonpath(ipmi_node, "$.sensor[?(@.['sensor_type']=='Temperature')]")
Run Code Online (Sandbox Code Playgroud)
When I remove the filter expression and just use "$.sensor.*" I get a list of all sensors, so I'm sure the …
我正在使用Spring的"spring-test-mvc"库来测试Web控制器.我有一个非常简单的控制器,它返回一个JSON数组.然后在我的测试中我有:
@Test
public void shouldGetAllUsersAsJson() throws Exception {
mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON))
.andExpect(content().mimeType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("fName").exists());
}
Run Code Online (Sandbox Code Playgroud)
以上测试返回:
java.lang.AssertionError: No value for JSON path: fName
Run Code Online (Sandbox Code Playgroud)
为了快速检查我实际得到的内容,我运行了以下测试:
@Test
public void shouldPrintResults() throws Exception {
mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON))
.andDo(print());
}
Run Code Online (Sandbox Code Playgroud)
它返回正文中的正确JSON数组 MockHttpServletResponse
我不确定为什么jsonPath无法fName在JSON数组中看到.
我正在尝试使用JSONPath(https://github.com/jayway/JsonPath)来搜索字段名称中包含空格的文档:
{
"model": {
"Details": {
"Nospace": "New today",
"Random nonsense": "New today"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用http://jsonpath.herokuapp.com/上的评估员进行测试
这有效:
$.model.Details[?(@.Nospace== 'New today')]
Run Code Online (Sandbox Code Playgroud)
但这不是:
$.model.Details[?(@.'Random nonsense'== 'New today')]
Run Code Online (Sandbox Code Playgroud)
这样做但缺少过滤器表达式:
$.model.Details['Random nonsense']
Run Code Online (Sandbox Code Playgroud)
因此,似乎可以引用带空格的字段,但我还没有找到如何在过滤器中使用它们.可能吗?我尝试了许多其他没有运气的组合,似乎也没有在网上找到任何关于它的东西.
谢谢.
我希望JsonPath始终将其解析为字符串的数据返回。但是,JsonPath read()方法的返回类型取决于要解析的数据类型,因为JsonPath总是猜测数据类型是什么,并返回该类型的结果。
问题是我无法知道将要返回哪种数据,因为我只能访问需要输入到read()函数中的一组路径。这意味着我必须将返回的JsonPath数据存储在Objects数组中,并使用包含instanceof关键字的if / else-if语句的丑陋集合来确定数据是什么(然后将其转换为适当的类型)。
有谁知道强制JsonPath返回字符串的方法?还是可以想到更好的解决方法?
这是我现在正在执行的操作(使用Java):
ArrayList<Object> realData = JsonPath.read(dataDumpFile, current.getPath());
Run Code Online (Sandbox Code Playgroud)
我想做的是:
ArrayList<String> realData = JsonPath.read(dataDumpFile, current.getPath());
Run Code Online (Sandbox Code Playgroud)
在文档中,我发现了这一点:
在Java中使用JsonPath时,重要的是要知道您期望的结果类型。JsonPath将自动尝试将结果转换为调用者期望的类型。
Run Code Online (Sandbox Code Playgroud)//Will throw an java.lang.ClassCastException List<String> list = JsonPath.parse(json).read("$.store.book[0].author") //Works fine String author = JsonPath.parse(json).read("$.store.book[0].author")
“调用者期望的类型”是什么意思?
我正在尝试使用 Json.NET 选择一些节点,SelectTokens它似乎不支持原始 jsonpath支持的相同语法。鉴于此输入:
{
"a": [
{
"id": 1
}
],
"b": [
{
"id": 2
},
{
"id": 3,
"c": {
"id": 4
}
}
],
"d": [
{
"id": 5
}
]
}
Run Code Online (Sandbox Code Playgroud)
a我只想要内部所有顶级对象的 id b,而不是内部对象的 id。使用 gossner 的解析器我可以这样做:$.[a,b].*.id,它返回[1, 2, 3]。
Json.NET 似乎既不支持逗号也不支持 *。如何使用 Json.NET 实现这一点?是否有关于 Json.NET jpath 选择器支持的内容的参考?
有没有办法通过过滤以下json中的“d”来提取“a”值?
[
{
"a":1,
"b":{ "c":11,"d":12 }
},
{
"a":2,
"b":{ "c":21,"d":22}
}
]
Run Code Online (Sandbox Code Playgroud) 我想使用“ kubectl秘密”中的所有键/值对进行打印。我无法弄清楚如何与-o --jsonpath标志一起一行或通过管道连接到jq。我当然可以编写一个脚本来执行此操作,但是我认为必须有一种更好的方法,因为kubernetes GUI在让您查看Secrets方面非常简单直接。
假设我这样创建秘密:
kubectl create secret generic testsecret --from-literal=key1=val1 --from-literal=key2=val2
现在,我可以运行以下kubectl get secret testsecret -o json命令:
{
"apiVersion": "v1",
"data": {
"key1": "dmFsMQ==",
"key2": "dmFsMg=="
},
...
}
Run Code Online (Sandbox Code Playgroud)
我可以做类似的事情
kubectl get secret testsecret -o jsonpath='{.data}'
要么
kubectl get secret testsecret -o json | jq '.data'
要以非列表格式获取键值对,则必须base64 --decode输入值。
清除所有键值对的最简单方法是什么?在所有机密上执行此操作的加分点(而不是像我在这里所做的那样仅针对一个特定的)。
我需要获取一个 json 字符串,它是更大 json 的一部分。举个简单的例子,我只想提取file01,我需要json对象作为字符串。
{
"file01": {
"id": "0001"
},
"file02": {
"id": "0002"
}
}
Run Code Online (Sandbox Code Playgroud)
所以,在代码中是这样的:
String file01 = JsonPath.parse(jsonFile).read("$.file01").toJson();
System.out.println(file01); // {"id":"0001"}
Run Code Online (Sandbox Code Playgroud)
我想使用库JsonPath,但我不知道如何获得我需要的东西。
任何帮助表示赞赏。谢谢!
根据GitHub 上的 JsonPath,应该可以访问数组的 max()、min() 或 sum(),但我不知道如何访问。使用此示例数据:
{
"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 …Run Code Online (Sandbox Code Playgroud) jsonpath ×10
java ×4
json ×4
base64 ×1
javascript ×1
jq ×1
json.net ×1
kubectl ×1
kubernetes ×1
mockito ×1
php ×1
python ×1
spring ×1
spring-test ×1