测试积点老人 发表于 2018-7-4 10:31:02

Day4-6测试积点任务

问题:
在以下的json中,我需要得到的几个字段是:当set.class.0非空的时候,返回该set列表中的某些字段
{
"code": 0,
"msg": null,
"data": {
"set": [
{
"id": "1566", "type": 1,
"class": { "0": [],"1": [ "460"],"2": ["460","460","461" ] },
},
{
"id": "1788", "type": 1,
"class": { "0": [ "461" ], "1": [], "2": [ "460" ]},
},
{
"id": "1762","type": 1,
"class": { "0": [],"1": [ "461" ], "2": [ "460" ],
},
{
"id": "1790","type": 1,
"class": { "0": [ "460","461"], "1": [], "2": [] },
},
{
"id": "1792","type": 2,
"class": {"0": [ "460", "461"], "1": [],"2": []
},
},]
}}
既是当存在"0": [ "461" ]与"0": [ "460", "461"]等”0“为非空内容时,取set中的id,type与”0"的属性值,在此例子中,则是得到id=1788,type=1,"0"=461。我用jsonpath仅仅是实现了当set.class.0非空的时候,返回整个set:
List<Object> list=JsonPath.read(string, "$.data.set[?(@.class.0.length()>0]");得到的结果是


{
"id": "1788", "type": 1,
"class": { "0": [ "461" ], "1": [], "2": [ "460" ]},
},
{
"id": "1790","type": 1,
"class": { "0": [ "460","461"], "1": [], "2": [] },
},
{
"id": "1792","type": 2,
"class": {"0": [ "460", "461"], "1": [],"2": []},
}
疑问:若有使用过jsonpath的人,可否回答下jsonpath是否还可以对已筛选的出的内容再次做检索。或者是还有其他的处理方法。非常感谢!



abcsell 发表于 2018-7-5 09:47:58

可以使用查询命令,重复递归需要保存以往的结果

jingzizx 发表于 2018-7-5 11:08:41

应该可以继续进行吧,做个测试就行啊

qqq911 发表于 2018-7-5 11:12:17

在for循环里面递归

104~牛牛 发表于 2018-7-5 14:39:13

已经筛选的结果是list,用list.get(0)的时候,输出的结果因为丢失了原本的双引号,使用jsonpath再处理的时候便报错了。我应该顺便再问多一个,转义问题,怎么保留原本的双引号。
页: [1]
查看完整版本: Day4-6测试积点任务