下面是我用来提取数据的实用程序的代码片段。 下面是示例JSON,我必须在其中提取“其他/车辆/属性”的值,但是却收到错误消息。
请提出我需要添加到实用程序的哪些内容? 我需要获取其他/车辆/属性的值。 请建议我需要在Utility中进行哪些更改,以便嵌套属性也可以从JSON中获取。
public class TestUtil {
public static JSONObject responsejson;
public static String getValueByJPath(JSONObject responsejson, String jpath){
Object obj = responsejson;
for(String s : jpath.split("/"))
if(!s.isEmpty())
if(!(s.contains("[") || s.contains("]")))
obj = ((JSONObject) obj).get(s);
else if(s.contains("[") || s.contains("]"))
obj = ((JSONArray) ((JSONObject) obj).get(s.split("\\[")[0])).get(Integer.parseInt(s.split("\\[")[1].replace("]", "")));
return obj.toString();
}
}
JSON格式:
{
"coverageEndDate": "02/28/2019",
"coverageStartDate": "03/01/2018",
"coverageType": "Personal",
"error": {
"code": 0,
"message": ""
},
"firstName": "Ronnie",
"insuredItems": {
"other": [{
"coverageEndDate": "02/28/2019",
"coverageStartDate": "03/01/2018",
"description": "Hunter Marin Passage 450",
"id": 14
}],
"properties": [{
"address": "31 LAKEWOOD RD, WEST BEND, WI 53090",
"coverageEndDate": "02/28/2019",
"coverageStartDate": "03/01/2018",
"deductible": 5000,
"description": "31 LAKEWOOD RD, WEST BEND, WI 53090",
"id": "113",
"limit": 2000000
}],
"vehicles": [{
"coverageEndDate": "02/28/2019",
"coverageStartDate": "03/01/2018",
"deductible": "$500 Comprehensive\n$500 Collision",
"description": "2016 Ford Explorer",
"id": "126",
"limit": "$100,000",
"make": "FORD",
"model": "EXPLORER",
"transportationExpense": "$30 per day and $900 max",
"vin": "1G1FA1E38F2131121",
"year": 2016
}, {
"coverageEndDate": "02/28/2019",
"coverageStartDate": "06/18/2018",
"deductible": "$500 Comprehensive\n$500 Collision",
"description": "2017 Dodg Durango",
"id": "127",
"limit": "$100,000",
"make": "DODG",
"model": "DURANGO",
"transportationExpense": "$30 per day and $900 max",
"vin": "2G1XER366DE4DE3H5",
"year": 2007
}]
},
"lastName": "James Dio",enter code here
"policyNumber": "5519930"
}