我有一个带嵌套字段的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
}
]
}
}
]
我正在使用JSONPATH从Value Rate
嵌套中获取Value Information
。
我已在此网站粘贴了我的JSON文字:http://jsonpath.com/ 并在使用此行后:
$[*].['Platform Compensation'].['Value Rate']
我得到了这个:
使用此行后:
$.['Value Information'].['Platform Compensation'].['Platform mission Id']
我得到了这个:
我想要返回(输出)的内容如下:
但我找不到合适的语法将这两者组合在一行中,并返回两个JSONPATH查询。
答案 0 :(得分:2)
jsonpath
可用于为给定的表达式选择值,并且在某些实现中,用于自定义谓词但不支持投影。
您可以使用jsonpath
过滤给定的JSON。例如:
返回包含所有Platform Compensation
值的数组:
$.['Value Information'].['Platform Compensation'].['Platform mission Id']
返回包含所有Platform mission Id
值的数组:
$.['Value Information'].['Platform Compensation']
但您无法使用jsonpath
来读取键和值的子集。要读取键和值的子集,您需要使用JSON de / serialization库。在Java世界中常用的库是Jackson和Gson。
以下是杰克逊的一个例子:
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);
答案 1 :(得分:2)
Jayway实施让你这样做。
jsonpath将是
$.[*]['Value Information']['Platform Compensation'][*]['Platform mission Id', 'Value Rate']