简体   繁体   中英

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

You need to go one level below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM