我有一个带有嵌套字段的 JSON:
[
{
"Platform Parent Dato Id": "23768",
"Platform Dato Id": "24138",
"Platform Dato Name": "Random Europe",
"Platform mission Id": "111112",
"Platform submission Id": "638687",
"Platform submission Flight Id": "863524",
"Start Date": "2017-12-01",
"End Date": "2017-12-02",
"Platform Compensation": 109.0909,
"Total Value": 909.0909,
"Goal": "200000.0000",
"Value Information": {
"Platform Compensation": [
{
"Platform mission Id": "111112",
"Platform submission Id": "638687",
"Platform submission Flight Id": "863524",
"Value Rate": "14.0000",
"Value": 109.0909
}
]
}
},
{
"Platform Parent Dato Id": "23768",
"Platform Dato Id": "24138",
"Platform Dato Name": "Random Europe",
"Platform mission Id": "111113",
"Platform submission Id": "638687",
"Platform submission Flight Id": "863524",
"Start Date": "2017-12-01",
"End Date": "2017-12-02",
"Platform Compensation": 109.0909,
"Total Value": 909.0909,
"Goal": "200000.0000",
"Value Information": {
"Platform Compensation": [
{
"Platform mission Id": "111113",
"Platform submission Id": "638687",
"Platform submission Flight Id": "863524",
"Value Rate": "12.0000",
"Value": 109.0909
}
]
}
}
]
Run Code Online (Sandbox Code Playgroud)
我正在使用 JSONPATH 以便Value Rate从Value Information巢中获取。
我已经在这个网站上粘贴了我的 JSON 文本:http : //jsonpath.com/ 并在使用这一行之后:
$[*].['Platform Compensation'].['Value Rate']
Run Code Online (Sandbox Code Playgroud)
我得到这个:
并在使用此行后:
$.['Value Information'].['Platform Compensation'].['Platform mission Id']
Run Code Online (Sandbox Code Playgroud)
我得到这个:
我试图返回(输出)如下:
但我找不到正确的语法将这两者组合在一行中,并用一个 JSONPATH 查询返回两者。
jsonpath可用于为给定表达式选择值,并且在某些实现中为自定义谓词选择值,但它不支持投影。
您可以使用jsonpath它来过滤给定的 JSON。例如:
返回包含所有值的数组Platform Compensation:
$.['Value Information'].['Platform Compensation'].['Platform mission Id']
Run Code Online (Sandbox Code Playgroud)返回包含所有值的数组Platform mission Id:
$.['Value Information'].['Platform Compensation']
Run Code Online (Sandbox Code Playgroud)但您不能用于读取键和值jsonpath的子集。要读取键和值的子集,您需要使用 JSON 反/序列化库。Java 世界中常用的库有Jackson和Gson。
这是使用 Jackson 的示例:
String json = "...";
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> asMap = mapper.readValue(json, Map.class);
Map<String, Object> transformed = new HashMap<>();
transformed.put("Platform mission Id", asMap.get("Platform mission Id"));
transformed.put("Value Rate", asMap.get("Value Rate"));
String result = mapper.writeValueAsString(transformed);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14579 次 |
| 最近记录: |