Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

JSONPath对于list的处理,当取值为null的时候,是否可以增加一个选项设置把null value设置到list里面,而不是直接忽略 #3607

Open
liverliu opened this issue Jan 5, 2021 · 2 comments

Comments

@liverliu
Copy link

liverliu commented Jan 5, 2021

对如下数据使用:JSONPath.eval(json, "$.data.dataRows[*].dataFields[*].value"),返回了6个元素的list,是否可以增加一种配置可以返回7个元素的list,其中首元素是null;当前默认的忽略处理,业务代码里面无法正确的映射元素的顺序,导致错误。

也就是希望有种方式,能让我取出的list是下面这种形式:
[null,"C3736",1621126800000,"**","29***6",1605456000000,"240397"]

而不是现在这样:
["C3736",1621126800000,"**","29***6",1605456000000,"240397"]

jayway的jsonpath是有这种能力的
Configuration conf = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
return JsonPath.using(conf).parse(json).read(path);

{
"data": {
"dataRows": [
{
"dataFields": [
{
"id": 1332,
"name": "gmtRegular",
"status": "success",
"valueType": "DATE"
},
{
"id": 302,
"name": "deptNo",
"status": "success",
"value": "C3736",
"valueType": "STRING"
},
{
"id": 143,
"name": "gmtOrigRegular",
"status": "success",
"value": 1621126800000,
"valueType": "DATE"
},
{
"id": 135,
"name": "name",
"status": "success",
"value": "",
"valueType": "STRING"
},
{
"id": 133,
"name": "workNo",
"status": "success",
"value": "29
*6",
"valueType": "STRING"
},
{
"id": 140,
"name": "gmtEntry",
"status": "success",
"value": 1605456000000,
"valueType": "DATE"
},
{
"id": 199,
"name": "superWorkNo",
"status": "success",
"value": "240397",
"valueType": "STRING"
}
]
}
]
},
"status": "success",
"success": true
}

@liverliu liverliu changed the title 对于list的处理,当取值为null的时候,是否可以增加一个选项设置把null value设置到list里面,而不是直接忽略 JSONPath对于list的处理,当取值为null的时候,是否可以增加一个选项设置把null value设置到list里面,而不是直接忽略 Jan 5, 2021
@github-ganyu
Copy link
Contributor

I think this is a good proposal and I try to submit a PR.

@github-ganyu
Copy link
Contributor

我写了一个重载方法,第二个参数传false代表不忽略空值:
JSONPath.eval(json, "$.data.dataRows[].dataFields[].value", false);
15ac107

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants