如何从android中的Json响应数组中获取值?

时间:2013-09-26 06:59:40

标签: android json

我是android和JSON解析的新手。在这里,我得到了我的json回复。现在我在jsonarray中有几个这样的回应。我想知道的是如何从这个响应中获取单个值。

即如何从这些响应中仅获取“id”:

 {"id":"c200","gender":"male","phone":{"office":"00 000000","home":"00 000000","mobile":"+91 0000000000"},"address":"xx-xx-xxxx,x - street, x - country","email":"ravi@gmail.com","name":"Ravi Tamada"}

{"id":"c201","gender":"male","phone":{"office":"00 000000","home":"00 000000","mobile":"+91 0000000000"},"address":"xx-xx-xxxx,x - street, x - country","email":"johnny_depp@gmail.com","name":"Johnny Depp"}

我只想要这两个响应的id。

我的代码是

JSONObject jobject = jparse.getJSONFromUrl(url);

    contacts = jobject.getJSONArray(TAG_CONTACTS);

    for(int i = 0 ; i < contacts.length() ; i++)
    {
        JSONObject c = contacts.getJSONObject(i);

        String id = c.getString(TAG_ID);

        Log.i("TAG", "STRING VALUE:" + contacts.getString(i));

                 String name = c.getString(TAG_NAME);
                 String email = c.getString(TAG_EMAIL);
                 String address = c.getString(TAG_ADDRESS);
                 String gender = c.getString(TAG_GENDER);
      }

2 个答案:

答案 0 :(得分:0)

如果你不想要所有id,那么删除for循环然后放

JSONObject j = contacts.getJSONObject(0);

String id = j.getString("id");

它将给出仅第一个对象id:c200

答案 1 :(得分:0)

你得到了json的服务器响应。 您只能获得ID,如下所示。

JSONObject json= new JSONObject(response);//response= your json string.

String id = json.getString("id");//id = param in your response