无法解析JSON,显示错误消息“JSONObject [”other“] not found”

问题描述 投票:-1回答:1

下面是我用来获取数据的实用程序的代码片段。下面是示例JSON,我必须获取其他/车辆/属性的值,但我收到一条错误消息。

请提出我需要添加到实用程序的内容?我需要获取其他/车辆/属性的值。请在实用程序中建议我需要更改的内容,以便嵌套属性也从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"
            }
java json automated-tests web-api-testing
1个回答
0
投票

使用来自this example的jackson lib的普通解析器

© www.soinside.com 2019 - 2024. All rights reserved.