0

我是 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);
      }
4

2 回答 2

0

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

JSONObject j = contacts.getJSONObject(0);

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

它会给出第一个对象ID,它只是:c200

于 2013-09-26T07:24:29.137 回答
0

您以 json 格式从服务器获得响应。您只能获取 id ,如下面的代码所示。

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

String id = json.getString("id");//id = param in your response
于 2013-09-26T07:38:20.997 回答