0

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

2
  • participate_users is nested within result. Commented Jan 9, 2014 at 14:44
  • argh.. yeah that's why... Thanks. Been looking for this bug for 30 mins now. Thanks Commented Jan 9, 2014 at 14:45

1 Answer 1

2

You need to go one level below.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yup thanks. for the answer. I shall accept it later. I guess iam just tired, that i overlooked that by mistake.

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.