如何从JSON对象中的键获取多个特定值

时间:2015-06-15 23:28:53

标签: java android json facebook facebook-graph-api

所以现在我正试图通过Facebook图表api从登录用户那里获取信息。

{"id":"10202628284866677","name":"Rohit Tigga","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/10202628284866677\/","picture":{"data":{"is_silhouette":false,"url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-xta1\/v\/t1.0-1\/p200x200\/10438582_10202555461926149_8285465463541146116_n.jpg?oh=283ec503c6eb051aad077366155c621d&oe=55E6E24B&__gda__=1445616180_3110e5bf89e9f3050fe623b86d47618b"}},"friends":{"data":[{"name":"Arthur Shir","id":"10153237510764181"},{"name":"Kanye East","id":"1454465268197338"}],"paging":{"next":"https:\/\/graph.facebook.com\/v2.3\/10202628284866677\/friends?access_token=CAAWKvUHZCc2sBAHDCZCfL1dHLcrrMoqNxHJSXun5vm1iJQlMxaF43ZCTvIj4Oo4sHeRstTEACDpDz45wLj9FwXBXkoqxPyBQvjcQKY4ZCPgEqhcPdyPb3JepOA1KCADTt2ZBIO8NRX8QKdrlvSWxxgEfSH6SY94nUsLZBRslP8OjJ9r9DYm6kih8GwJ7la0w30KbZAi3AFNOUx1FDU9Y38S&limit=25&offset=25&__after_id=enc_AdB7OPjfCizP7TWz68NToMvADpaegj9bV3w8g22S1dgcGTiHSRUC2xQr2uhJDdQczRtmkKPQS9fzh0Fq7LgWLZAsu"},"summary":{"total_count":1289}}}

我能够获得id,name,profile_pic_url

GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {
                                        // Application code
                                        response.getError();
                                        //Here's what you got
                                        Log.e("JSON:", object.toString());

                                        //Find out how to store all user friends efficiently

                                        try {
                                            user_id=object.getString("id");
                                            name=object.getString("name");
                                            profile=object.getString("link");
                                            profile_pic_url = object.getString("picture");
                                            friends = object.getString("friends");


                                            JSONObject pic_object1= (JSONObject) new JSONTokener(profile_pic_url).nextValue();
                                            JSONObject pic_object2 = pic_object1.getJSONObject("data");
                                            profile_pic_url = (String) pic_object2.get("url");

我更具体地试图从登录用户获取每个facebook好友的ID。它来自我现在作为一个字符串的这一部分。

 {"data":[{"name":"Arthur Shir","id":"10153237510764181"},{"name":"Kanye East","id":"1454465268197338"}],"paging":{"next":"https:\/\/graph.facebook.com\/v2.3\/10202628284866677\/friends?access_token=CAAWKvUHZCc2sBAHDCZCfL1dHLcrrMoqNxHJSXun5vm1iJQlMxaF43ZCTvIj4Oo4sHeRstTEACDpDz45wLj9FwXBXkoqxPyBQvjcQKY4ZCPgEqhcPdyPb3JepOA1KCADTt2ZBIO8NRX8QKdrlvSWxxgEfSH6SY94nUsLZBRslP8OjJ9r9DYm6kih8GwJ7la0w30KbZAi3AFNOUx1FDU9Y38S&limit=25&offset=25&__after_id=enc_AdB7OPjfCizP7TWz68NToMvADpaegj9bV3w8g22S1dgcGTiHSRUC2xQr2uhJDdQczRtmkKPQS9fzh0Fq7LgWLZAsu"},"summary":{"total_count":1289}}

我正在尝试遍历JSON对象并将每个id存储在一个数组中。

但我的尝试没有成功。

我正在看这个,但没有运气:/

How to iterate over a JSONObject?

How can I iterate JSONObject to get individual items

我应该怎么做呢?

2 个答案:

答案 0 :(得分:1)

你应该能够从像这样的JSON中获取JSONArray“数据”

JSONArray array = object.getJSONArray("data");

for(int i = 0; i < array.length; i++) {
    JSONObject object = (JSONObject) array.get(i);

    String id = object.get("id");
    //and so on
}

答案 1 :(得分:1)

您的目标是从以下json字符串中检索ID:

{"data":[{"name":"Arthur Shir","id":"10153237510764181"},{"name":"Kanye East","id":"1454465268197338"}],"paging":{"next":"https:\/\/graph.facebook.com\/v2.3\/10202628284866677\/friends?access_token=CAAWKvUHZCc2sBAHDCZCfL1dHLcrrMoqNxHJSXun5vm1iJQlMxaF43ZCTvIj4Oo4sHeRstTEACDpDz45wLj9FwXBXkoqxPyBQvjcQKY4ZCPgEqhcPdyPb3JepOA1KCADTt2ZBIO8NRX8QKdrlvSWxxgEfSH6SY94nUsLZBRslP8OjJ9r9DYm6kih8GwJ7la0w30KbZAi3AFNOUx1FDU9Y38S&limit=25&offset=25&__after_id=enc_AdB7OPjfCizP7TWz68NToMvADpaegj9bV3w8g22S1dgcGTiHSRUC2xQr2uhJDdQczRtmkKPQS9fzh0Fq7LgWLZAsu"},"summary":{"total_count":1289}}

在这种情况下,您可以尝试以下代码:

JSONObject jsonObject = new JSONObject(jsonString);
JSONArray friends = jsonObject.getJSONArray("data");
for (int index=0; index<friends.length(); ++index){
        JSONObject currentFriend = friends.getJSONObject(index);
        String id = currentFriend.getString("id");
}