MR Mido
MR Mido

Reputation: 1678

Trouble parsing this json response

I'm trying to parse the following json response:

[
    {
        "modelTrimList":{
            "modelTrim":[
                {
                    "bodyStyle":"5-DOOR",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6203,
                    "transmission":"Manual",
                    "cityMpg":0,
                    "msrp":16000,
                    "description":"5-Door",
                    "modelYear":2011,
                    "name":"Scion xb",
                    "curbWeight":0,
                    "@id":"81454531",
                    "effectiveDate":"4\/28\/10",
                    "displayName":"xB",
                    "deliveryFee":775
                },
                {
                    "bodyStyle":"5-DOOR",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6202,
                    "transmission":"Automatic",
                    "cityMpg":0,
                    "msrp":16950,
                    "description":"5-DOOR",
                    "modelYear":2011,
                    "name":"Scion xb",
                    "curbWeight":0,
                    "@id":"81454516",
                    "effectiveDate":"4\/28\/10",
                    "displayName":"xB",
                    "deliveryFee":720
                }
            ]
        },
        "name":"xB",
        "@id":"Scion xb"
    },
    {
        "modelTrimList":{
            "modelTrim":[
                {
                    "bodyStyle":"3 DOOR LIFTBACK",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6223,
                    "transmission":"Manual",
                    "cityMpg":0,
                    "msrp":18275,
                    "description":"2 DOOR L\/B",
                    "modelYear":2011,
                    "name":"Scion tC",
                    "curbWeight":3945,
                    "@id":"84604049",
                    "effectiveDate":"8\/6\/10",
                    "displayName":"tC",
                    "deliveryFee":720
                },
                {
                    "bodyStyle":"3 DOOR LIFTBACK",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6222,
                    "transmission":"Automatic",
                    "cityMpg":0,
                    "msrp":19275,
                    "description":"2 DOOR L\/B",
                    "modelYear":2011,
                    "name":"Scion tC",
                    "curbWeight":3945,
                    "@id":"84604028",
                    "effectiveDate":"8\/6\/10",
                    "displayName":"tC",
                    "deliveryFee":720
                }
            ]
        },
        "name":"tC",
        "@id":"Scion tC"
    },
    {
        "modelTrimList":{
            "modelTrim":[
                {
                    "bodyStyle":"5-DOOR",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6233,
                    "transmission":"Manual",
                    "cityMpg":0,
                    "msrp":15045,
                    "description":"5-DOOR",
                    "modelYear":2011,
                    "name":"Scion xd",
                    "curbWeight":3605,
                    "@id":"91724869",
                    "effectiveDate":"12\/8\/10",
                    "displayName":"xD",
                    "deliveryFee":775
                },
                {
                    "bodyStyle":"5-DOOR",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6232,
                    "transmission":"Automatic",
                    "cityMpg":0,
                    "msrp":15845,
                    "description":"5-DOOR",
                    "modelYear":2011,
                    "name":"Scion xd",
                    "curbWeight":3605,
                    "@id":"91724562",
                    "effectiveDate":"12\/8\/10",
                    "displayName":"xD",
                    "deliveryFee":775
                }
            ]
        },
        "name":"xD",
        "@id":"Scion xd"
    }
]

I can't seem to be parsing it correctly please advice and if possible a sample code that I could use, any ideas are more than welcome.

Upvotes: 0

Views: 270

Answers (3)

Femi
Femi

Reputation: 64700

You need to wrap it with []: right now it is not valid JSON.

EDIT: if it is properly wrapped, try this:

JSONArray js = new JSONArray(newjson);
        Toast.makeText(getApplicationContext(),js.toString(), Toast.LENGTH_LONG).show();
        for (int i = 0; i < js.length(); i++) {
            Toast.makeText(getApplicationContext(),js.getJSONObject(i).getString("name").toString(), Toast.LENGTH_LONG)
            .show();

Upvotes: 2

user2043
user2043

Reputation:

Easiest way to answer this is to run it through an online validation tool such as JSON Lint:

http://jsonlint.com/

With your example it suggested that you have an error on line 42:

Parse error on line 42: ... "@id":"Scion xb"},{ "modelTrimLi ----------------------^ Expecting 'EOF'

Upvotes: 0

no.good.at.coding
no.good.at.coding

Reputation: 20371

That's because your JSON isn't valid, it looks like it should be an array of modelTrimLists

Upvotes: 0

Related Questions