3

I am aware there are duplicate questions. I am not able to figure this out despite that.

Below is a JSON file which I receive from an API. I need to get the "year" value.

The error I am getting is:

message org.glassfish.jersey.server.ContainerException: org.json.JSONException: JSONArray[1] not found.

Which relates to the line on Java:

JSONObject year = years.getJSONObject(1).getJSONObject("years");

I also tried:

JSONObject year = years.getJSONObject(1);

Why is this code not working? Isn't index 1 clearly year in the years array?

JSON

{
    "make": {
        "id": 200000404,
        "name": "Chevrolet",
        "niceName": "chevrolet"
    },
    "model": {
        "id": "Chevrolet_Camaro",
        "name": "Camaro",
        "niceName": "camaro"
    },
    "drivenWheels": "rear wheel drive",
    "numOfDoors": "2",
    "options": [],
    "colors": [],
    "manufacturerCode": "1EH67",
    "price": {
        "baseMSRP": 34180.0,
        "baseInvoice": 32813.0,
        "deliveryCharges": 900.0,
        "usedTmvRetail": 17766.0,
        "usedPrivateParty": 16321.0,
        "usedTradeIn": 14755.0,
        "estimateTmv": false,
        "tmvRecommendedRating": 0
    },
    "categories": {
        "market": "Performance",
        "EPAClass": "Compact Cars",
        "vehicleSize": "Midsize",
        "primaryBodyType": "Car",
        "vehicleStyle": "Convertible",
        "vehicleType": "Car"
    },
    "vin": "2G1FC3D33C9165616",
    "squishVin": "2G1FC3D3C9",
    "years": [{
        "id": 100531911,
        "year": 2012,
        "styles": [{
            "id": 101395591,
            "name": "LT 2dr Convertible w/2LT (3.6L 6cyl 6M)",
            "submodel": {
                "body": "Convertible",
                "modelName": "Camaro Convertible",
                "niceName": "convertible"
            },
            "trim": "LT"
        }]
    }],
    "matchingType": "SQUISHVIN",
    "MPG": {
        "highway": "28",
        "city": "17"
    }
}

JAVA

    public String vehicleData(@PathParam("vin") String vin,
                               @PathParam("key") String key) throws Exception {

        GetVehicleJSON jsonData = new GetVehicleJSON(vin, key);
        JSONObject data = jsonData.getVehicleData();

        String name = data.getJSONObject("make").getString("name"); 

        String highway = data.getJSONObject("MPG").getString("highway");
        String city = data.getJSONObject("MPG").getString("city");

        JSONArray years = data.getJSONArray("years");
        JSONObject year = years.getJSONObject(1).getJSONObject("years");
        String s = year.getString("year");

        return name + " " + s + " " + highway + " " + city;
    }
6
  • 1
    Actually, 1 points to the second element in the array, which doesn't exist in this case. 0 points to the first JSONObject, which contains year. So you want years.getJSONObject(0). Commented Mar 6, 2016 at 5:40
  • 1
    Try JSONObject year = years.getJSONObject(0);. Array indexing generally starts at 0, not 1. Commented Mar 6, 2016 at 5:40
  • @Jonathan Lonowski isn't the "id" object at the 0 index? Commented Mar 6, 2016 at 5:41
  • 1
    @NutellaAddict. years is a JSONArray which is an array containing JSONObjects. In your example, this JSONArray, [ ], contains one JSONObject, { .. }. Within this one object you have, id, year, etc. So it is JSONArray -> JSONObject -> year. Commented Mar 6, 2016 at 5:43
  • 1
    @NutellaAddict No. "years" refers to an Array ([...]) holding a single Object ({"id":...}). The 0 index of the Array will give you that Object. Then, from that, you can retrieve the "year" as a String. Commented Mar 6, 2016 at 5:43

2 Answers 2

3

You're working with JSON array whose indexing is zero-based. Do it as:

   JSONArray years = data.getJSONArray("years");

   String year = years.getJSONObject(0).getString("year");
Sign up to request clarification or add additional context in comments.

Comments

2

Your json shows that the years value is an array with 1 element and inside that element is an object:

"years": [{ "id": 100531911, "year": 2012, "styles": [{ "id": 101395591, "name": "LT 2dr Convertible w/2LT (3.6L 6cyl 6M)", "submodel": { "body": "Convertible", "modelName": "Camaro Convertible", "niceName": "convertible" }, "trim": "LT" }] }],

Because array element numbering begins at 0, that means if you select element 1 you will get this error:

message org.glassfish.jersey.server.ContainerException: org.json.JSONException: JSONArray[1] not found.

To fix it you need to select element 0 instead of 1:

years.getJSONObject(0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.