无法获取JSON对象类型数组

时间:2014-01-09 14:44:09

标签: java android json arrays

我有以下代码

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;
}

以下JSON:

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

但是我收到以下错误:

org.json.JSONException: No value for participate_users

我有一种感觉,我不小心忽略了JSON格式。有人可以指出我正确的方向吗?

由于

1 个答案:

答案 0 :(得分:2)

你需要在下面一层。

试试这个 - JSONArray jPartyArr = jObj.getJSONObject("result").getJSONArray("participate_users");