-1

Below is the code snippet for the utility which I've used to fetch data. Below are sample JSON in which I have to fetch the value of Other/Vehicle/Properties, but I'm getting an error message.

Please suggest what things I need to add to the utility? I need to get the value of Other/Vehicle/Properties. Please suggest what things I need to change in the Utility so that nested property also fetched from 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 Format:

    {
                "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"
            }
Joseph Cho
  • 4,033
  • 4
  • 26
  • 33

1 Answers1

0

Use normal parser from jackson lib by this example

Steklorez
  • 109
  • 1
  • 4