从JSONArray获取喜欢数据(id和名称) - Facebook api

时间:2017-12-27 08:38:02

标签: android facebook facebook-graph-api

我正试图在脸谱图api on androi上的喜欢数据中获取ID和名称 d:

{
   "data": [
      {
         "id": "1433908706707486_917483885016640"
      },
      {
         "id": "1433908706707486_910831695681859"
      },
      {
         "id": "1433908706707486_906550586109970",
         "likes": {
            "data": [
               {
                  "id": "xxxx",
                  "name": "xxx"
               }
            ],
            "paging": {
               "cursors": {
                  "before": "MTEwNTU0NTg3OTQ2NjE3NgZDZD",
                  "after": "MTEwNTU0NTg3OTQ2NjE3NgZDZD"
               }
            }
         }
      },
      {
         "id": "1433908706707486_902931413138554",
         "likes": {
            "data": [
               {
                  "id": "xxx",
                  "name": "Muhammad Waseem"
               }
            ],
            "paging": {
               "cursors": {
                  "before": "NzI3NTMxNjA0MDA0MDA5",
                  "after": "NzI3NTMxNjA0MDA0MDA5"
               }
            }
         }
      },
      {
         "id": "1433908706707486_134266116671758"
      }
   ],
   "paging": {
      "previous": "https://graph.faceb....",
      "next": "https://graph.faceb...
   }
}

问题在于:

12-27 14:56:45.542 21352-21352/com.facebook.pv2 W/System.err: org.json.JSONException: Unterminated object at character 25 of {Response:  responseCode: 200, graphObject: {"data":[{"id":"1781423402174843_1912187512431764"},{"id":"1781423402174843_1906871369630045","likes":{"data":[{"id":"xxx","name":"Umer Hameed"}],"paging":{"cursors":{"after":"NjM5NTg5OTE5NTY0Njk0","before":"NjM5NTg5OTE5NTY0Njk0"}}}},{"id":"1781423402174843_1905874473063068"},{"id":"1781423402174843_1904892806494568","likes":{"data":[{"id":"xxx","name":"Shafiqe Ali"},{"id":"xxx","name":"Ahmad Chauhan"},{"id":"xxx","name":"Umer Hameed"},{"id":"xxx","name":"Arslan Mughal"},{"id":"xxxx","name":"Zahid Rachid"}],"paging":{"cursors":{"after":"NDQ1NTMxMzQ5MTEzNjc0","before":"MTEzNTM5MDgzMzIzOTc4NgZDZD"}}}},{"id":"1781423402174843_1904460023204513","likes":{"data":[{"id":"xxx","name":"Waqas Sunny"},{"id":"xxxx","name":"Arslan Mughal"},{"id":"xxxx","name":"Ahmad Chauhan"}],"paging":{"cursors":{"after":"MTAzMzM2MTg0MzQ2MTk2NwZDZD","before":"Nzc2NDM4NjYyNTEzNDc4"}}}},{"id":"1781423402174843_1904368313213684","likes":{"data":[{"id":"xxxx","name":"Ahmad Chauhan"}],"paging":{"cursors":{"after":"MTAzMzM2MTg0MzQ2MTk2NwZDZD","before":"MTAzMzM2MTg0MzQ2MTk2NwZDZD"}}}},{"id":"1781423402174843_1883311385319377","likes":{"data":[{"id":"1033361843461967","name":"Ahmad Chauhan"},{"id":"639589919564694","name":"Umer Hameed"}],"paging":{"cursors":{"after":"NjM5NTg5OTE5NTY0Njk0","before":"MTAzMzM2MTg0MzQ2MTk2NwZDZD"}}}},{"id":"1781423402174843_1852042315112951","likes":{"data":[{"id":"1263530067099941","name":"Arslan Mughal"},{"id":"1135390833239786","name":"Shafiqe Ali"},{"id":"1033361843461967","name":"Ahmad Chauhan"}],"paging":{"cursors":{"after":"MTAzMzM2MTg0MzQ2MTk2NwZDZD","before":"MTI2MzUzMDA2NzA5OTk0MQZDZD"}}}},{"id":"1781423402174843_1850409191942930","likes":{"data":[{"id":"xxx","name":"Nasir Mughal"},{"id":"xxxx","name":"Shafiqe Ali"},{"id":"xxxx","name":"Taha Sheikh"},{"id":"xxxx","name":"Waqas Sunny"},{"id":"xxxx","name":"Arslan Mughal"},{"id":"xxxx","name":"Zahid Rachid"}],"paging":{"cursors":{"after":"NDQ1NTMxMzQ5MTEzNjc0","before":"MTA4NTMyMzEzODIzOTYxNAZDZD"}}}},{"id":"1781423402174843_1849173595399823","likes":{"data":[{"id":"xxxx","name":"Umer Hameed"},{"id":"xxxx","name":"Waqas Sunny"},{"id":"xxxx","name":"Shafiqe Ali"},{"id":"xxxx","name":"Arslan Mughal"}],"paging":{"cursors":{"after":"MTI2MzUzMDA2NzA5OTk0MQZDZD","before":"NjM5NTg5OTE5NTY0Njk0"}}}},{"id":"1781423402174843_1847584705558712"},{"id":"1781423402174843_1845844379066078","likes":{"data":[{"id":"445531349113674","name":"Zahid Rachid"}],"paging":{"cursors":{"after":"NDQ1NTMxMzQ5MTEzNjc0","before":"NDQ1NTMxMzQ5MTEzNjc0"}}}},{"id":"1781423402174843_1837370286580154","likes":{"data":[{"id":"1135390833239786","name":"Shafiqe Ali"},{"id":"455768698087604","name":"Usama Ishaq"}],"paging":{"cursors":{"after":"NDU1NzY4Njk4MDg3NjA0","before":"MTEzNTM5MDgzMzIzOTc4NgZDZD"}}}},{"id":"1781423402174843_1832619910388525"},{"id":"1781423402174843_1828230240827492"},{"id":"1781423402174843_1828176680832848"},{"id":"1781423402174843_1820949544888895"},{"id":"1781423402174843_1807792559537927","likes":{"data":[{"id":"1120628128049019","name":"Sheheryar Ali Mehar"},{"id":"1033361843461967","name":"Ahmad Chauhan"},{"id":"776438662513478","name":"Waqas Sunny"},{"id":"445531349113674","name":"Zahid Rachid"}],"paging":{"cursors":{"after":"NDQ1NTMxMzQ5MTEzNjc0","before":"MTEyMDYyODEyODA0OTAxOQZDZD"}}}},{"id":"1781423402174843_1790316361285547"},{"id":"1781423402174843_1787495658234284"},{"id":"1781423402174843_1780168658966984","likes":{"data":[{"id":"xxxx","name":"Tahir Farooq"},{"id":"xxxx","name":"Shafiqe Ali"},{"id":"xxxx","name":"Arslan Mughal"}],"paging":{"cursors":{"after":"MTI2MzUzMDA2NzA5OTk0MQZDZD","before":"MTAyMTA4NTE2NzY3NTk1MTEZD"}}}},{"id":"1781423402174843_1775850492732

我已经批准了facebook的user_posts权限,我的代码只收到了用户的帖子,请帮助我注意喜欢我的帖子的ID和姓名。感谢

2 个答案:

答案 0 :(得分:0)

试试这个

try {

        JSONObject resObject = new JSONObject("your response");
        JSONArray jsonArray = resObject.getJSONArray("data");

        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);

            Log.i("ID", "-->" + jsonObject.getString("id"));
            Log.i("Likes", "-->" + jsonObject.getString("likes"));

            JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");

            JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");

            for (int j = 0; j < jsonArrayLikeData.length(); j++) {

                JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(i);
                Log.i("Like ID", "-->" + jsonObjectlike.getString("id"));
                Log.i("Like Name", "-->" + jsonObjectlike.getString("name"));
            }


        }


    }
    catch (JSONException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

修改

new GraphRequest(
        AccessToken.getCurrentAccessToken(),
"/me/posts",
parameters,
HttpMethod.GET,
        new GraphRequest.Callback() {
    public void onCompleted(GraphResponse response) {
    /* handle the result */
        //Log.v("TAG", "Facebook Photos response: " + response);

        try {
            if (response.getError() == null) {


                JSONObject resObject = new JSONObject(response);
                JSONArray jsonArray = resObject.getJSONArray("data");

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    Log.i("ID", "-->" + jsonObject.getString("id"));
                    Log.i("Likes", "-->" + jsonObject.getString("likes"));

                    JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");

                    JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");

                    for (int j = 0; j < jsonArrayLikeData.length(); j++) {

                        JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(i);
                        Log.i("Like ID", "-->" + jsonObjectlike.getString("id"));
                        Log.i("Like Name", "-->" + jsonObjectlike.getString("name"));
                    }


                }

                //set your adapter here

            } else {
                Log.v("TAG", response.getError().toString());
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}).executeAsync();

编辑新

try 
{
    JSONObject resObject = new JSONObject("your response");
    JSONArray jsonArray = resObject.getJSONArray("data");


        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            Log.i("ID", "-->" + jsonObject.getString("id"));

            if(jsonArray.getJSONObject(i).has("likes"))
            {

                JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");

                JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");

                for (int j = 0; j < jsonArrayLikeData.length(); j++) {

                    JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(j);

                    Log.i("Like ID", "-->" + jsonObjectlike.getString("id"));
                    Log.i("Like Name", "-->" + jsonObjectlike.getString("name"));
                }

            }



        }
}
catch (JSONException e)
{
     // TODO Auto-generated catch block
     e.printStackTrace();
}

<强>输出

enter image description here

答案 1 :(得分:0)

JSONObject object = new JSONObject("YOUR_RESPONSE");
        if (object.has("data")) {
            JSONArray jaData = object.getJSONArray("data");
            for (int i = 0; i < jaData.length(); i++) {
                JSONObject likeObject = jaData.getJSONObject(i).getJSONObject("likes");
                JSONArray likeDeatilArray = likeObject.getJSONArray("data");
                for (int j = 0; j < likeDeatilArray.length(); j++) {
                    String id = likeDeatilArray.getJSONObject(j).optString("id");
                    String name =  likeDeatilArray.getJSONObject(j).optString("name");

                    Log.v("name -", id+" name -"+name);
                }
            }

        }

希望这会有所帮助..