Jeremy
Jeremy

Reputation: 2536

Unable to get JSON Object type array

I have the following code

public static ArrayList<NameValuePair> getParticipants(String jData)throws JSONException{
    JSONObject jObj = new JSONObject(jData);
    ArrayList<NameValuePair> partyList = new ArrayList<NameValuePair>();
    JSONArray jPartyArr = jObj.getJSONArray("participate_users"); <-- ERROR HERE

    servResponse = (int) jObj.getInt("returnCode");
    if(servResponse == 1){
        for(int i=0; i<jPartyArr.length(); i++){
            JSONObject obj = jPartyArr.getJSONObject(i);
            partyList.add(new BasicNameValuePair("account", obj.getString("account_id")));
            partyList.add(new BasicNameValuePair("name", obj.getString("realname")));
        }

    }
    return partyList;
}

And the following JSON:

{
    "returnCode": "1",
    "result": {
        "chat_group_id": "82",
        "participate_users": [
            {
                "account_id": "328",
                "realname": "Jessica"
            },
            {
                "account_id": "360",
                "realname": "Chloe"
            }
        ]
    }
}

However i get the following error:

org.json.JSONException: No value for participate_users

I have a feeling that i accidentally overlooked with the JSON formatting. Can anyone please point me to the right direction?

Thanks

Upvotes: 0

Views: 166

Answers (1)

JHS
JHS

Reputation: 7871

You need to go one level below.

Try this - JSONArray jPartyArr = jObj.getJSONObject("result").getJSONArray("participate_users");

Upvotes: 2

Related Questions