使用Fql并尝试使用JSON解析结果

时间:2014-01-10 09:29:42

标签: java android json facebook facebook-fql

我有一个Fql,它会生成以下结果,我可以记录它,以及我从中复制它的地方。

01-10 14:53:46.231: I/System.out(14104): {Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[{"fql_result_set":[{"uid2":"2910762"},{"uid2":"504357090"},{"uid2":"505589742"},{"uid2":"514537477"},{"uid2":"520769196"},{"uid2":"523017182"},{"uid2":"523381317"},{"uid2":"524516226"},{"uid2":"534408772"},{"uid2":"536564273"},{"uid2":"538592962"},{"uid2":"539693868"},{"uid2":"553628950"},{"uid2":"562208973"},{"uid2":"567118982"},{"uid2":"581078622"},{"uid2":"583635483"},{"uid2":"597966653"},{"uid2":"598643977"},{"uid2":"599257658"},{"uid2":"600858170"},{"uid2":"601805231"},{"uid2":"607723205"},{"uid2":"608304436"},{"uid2":"609029380"}],"name":"friends"},{"fql_result_set":[{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash1\/203042_2910762_1470074612_q.jpg","uid":2910762,"birthday":null,"name":"Abir Bazaz"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash2\/1118052_504357090_120550829_q.jpg","uid":504357090,"birthday":"August 20","name":"חביב עבדול סטאר"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash2\/1118654_505589742_1874715172_q.jpg","uid":505589742,"birthday":null,"name":"Khalid Ahamed"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash2\/1116700_514537477_79461990_q.jpg","uid":514537477,"birthday":"July 23","name":"Sudeep Kane"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-prn2\/276216_520769196_767587364_q.jpg","uid":520769196,"birthday":"November 7","name":"AsMa Aziz"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-prn2\/1086385_523017182_2040085401_q.jpg","uid":523017182,"birthday":null,"name":"Ubaid Shah"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash2\/1118825_523381317_1049463146_q.jpg","uid":523381317,"birthday":"January 29, 1993","name":"Zeeshaan Shabbir"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash1\/260756_524516226_49852804_q.jpg","uid":524516226,"birthday":"February 16, 1985","name":"Bhushan Deshmukh"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash2\/370117_534408772_962890658_q.jpg","uid":534408772,"birthday":"November 29","name":"Arjimand Hussain Talib"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-frc1\/369651_536564273_1193996674_q.jpg","uid":536564273,"birthday":"October 2, 1988","name":"Kaustubh Mahajan"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash2\/1118800_538592962_348969657_q.jpg","uid":538592962,"birthday":"September 23","name":"Talat Bhat"},{"pic_square":"https:\/\/scontent-b.xx.fbcdn.net\/hprofile-ash1\/276362_539693868_1660054053_q.jpg","uid":539693868,"birthday":"March 31","name":"Jimmy Feenix"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-prn2\/1119371_553628950_1921126147_q.jpg","uid":553628950,"birthday":"September 3","name":"Mohamad Hazic"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash1\/187700_562208973_3134355_q.jpg","uid":562208973,"birthday":"May 17","name":"Vivek Mishra"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yo\/r\/UlIqmHJn-SK.gif","uid":567118982,"birthday":null,"name":"Usman Abdullah Nehru"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash1\/211914_581078622_1190090055_q.jpg","uid":581078622,"birthday":"July 31, 1987","name":"Atman Khanna"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-prn2\/t5\/1086502_583635483_389447379_q.jpg","uid":583635483,"birthday":"December 22","name":"Tanvi Wadhwa"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash3\/157544_597966653_461166687_q.jpg","uid":597966653,"birthday":null,"name":"Adnan Yousseff"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash1\/211726_598643977_1428225594_q.jpg","uid":598643977,"birthday":null,"name":"Farhan Jehanzeb Naqash"},{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash3\/t5\/41436_599257658_2934_q.jpg","uid":599257658,"birthday":

现在我有以下函数来解析这个结果:

public void onCompleted(Response response) {


                        GraphObject graphObject = response.getGraphObject();


                        if (graphObject != null)
                        {
                            if (graphObject.getProperty("data") != null)
                            {
                                try {
                                    String arry = graphObject.getProperty("data").toString();

                                    JSONArray jsonNArray = new JSONArray(arry);

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

                                        JSONObject jsonObject = jsonNArray.getJSONObject(i);

                                        String name = jsonObject.getString("name");
                                        String uid = jsonObject.getString("uid");

                                        String pic_square = jsonObject.getString("pic_square");
                                        String status = jsonObject.getString("birthday");

                                        Log.i("Entry\n", "\nuid: " + uid + ",\n name: " + name + ",\n pic_square: " + pic_square + ", \nstatus: " + status);
                                    }

                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }                                       
                            }
                        } 
                        TV.setText(response.toString()); 
                        System.out.println(response.toString());
                    }                
                }); 
                Request.executeBatchAsync(request);                 
            }
        });

结果:

01-10 14:58:03.961: W/System.err(14374): org.json.JSONException: No value for uid
01-10 14:58:03.962: W/System.err(14374):    at org.json.JSONObject.get(JSONObject.java:354)
01-10 14:58:03.962: W/System.err(14374):    at org.json.JSONObject.getString(JSONObject.java:510)
01-10 14:58:03.962: W/System.err(14374):    at com.testbdr.MainFragment$3$1.onCompleted(MainFragment.java:126)
01-10 14:58:03.962: W/System.err(14374):    at com.facebook.Request$4.run(Request.java:1669)
01-10 14:58:03.962: W/System.err(14374):    at android.os.Handler.handleCallback(Handler.java:615)
01-10 14:58:03.963: W/System.err(14374):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 14:58:03.963: W/System.err(14374):    at android.os.Looper.loop(Looper.java:153)
01-10 14:58:03.963: W/System.err(14374):    at android.app.ActivityThread.main(ActivityThread.java:5000)
01-10 14:58:03.963: W/System.err(14374):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 14:58:03.963: W/System.err(14374):    at java.lang.reflect.Method.invoke(Method.java:511)
01-10 14:58:03.963: W/System.err(14374):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-10 14:58:03.963: W/System.err(14374):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-10 14:58:03.963: W/System.err(14374):    at dalvik.system.NativeStart.main(Native Method)

FQL是否返回格式错误的JSON?或者我的错在哪里?

1 个答案:

答案 0 :(得分:3)

只需替换

 String arry = graphObject.getProperty("data").toString();
 JSONArray jsonNArray = new JSONArray(arry);

这一个

    String arry = graphObject.getProperty("data").toString();
    JSONArray jsonNArray1 = new JSONArray(arry);
    JSONArray jsonNArray = new JSONArray();
    if (jsonNArray1.length() > 0) {
        JSONObject jsonObject1 = jsonNArray1.getJSONObject(1);
        jsonNArray = jsonObject1.getJSONArray("fql_result_set");
    }

我希望如果您获得正确的数据,那么您的概率可能会得到解决。