2013-06-02 111 views
-1

我正在爲Facebook應用程序開發Facebook應用程序。我查詢用戶得分訪問用戶圖:解析Facebook圖形API響應

https://graph.facebook.com/user_id/scores&access_token={user_access_token}

我得到這樣的迴應:

{ 
"data": [ 
{ 
    "user": { 
    "name": "Michał Szydłowski", 
    "id": "100001699654797" 
    }, 
    "score": 1200, 
    "application": { 
    "name": "QuizzlePeople", 
    "namespace": "quizzlepeople", 
    "id": "176694722487191" 
    } 
}, 
{ 
    "user": { 
    "name": "Michał Szydłowski", 
    "id": "100001699654797" 
    }, 
    "score": 1, 
    "application": { 
    "name": "Friend Smash!", 
    "namespace": "friendsmashsample", 
    "id": "480369938658210" 
    } 
}, 
{ 
    "user": { 
    "name": "Michał Szydłowski", 
    "id": "100001699654797" 
    }, 
    "score": 0, 
    "application": { 
    "name": "Wordox", 
    "namespace": "wordox-the-game", 
    "id": "227993680602156" 
    } 
}, 
{ 
    "user": { 
    "name": "Michał Szydłowski", 
    "id": "100001699654797" 
    }, 
    "score": 0, 
    "application": { 
    "name": "SongPop", 
    "namespace": "songpop", 
    "id": "323595464361460" 
    } 
} 
] 
} 

我如何提取此有用的數據?

我試圖把一些東西作爲一個JSONObject,使用:

HttpClient client = new DefaultHttpClient();     
     HttpGet get = new HttpGet("https://graph.facebook.com/me?fields=score&access_token=" + token); 
     HttpResponse resp = client.execute(get);   
     Log.i("info1", resp.toString()); 
     // Parse the response 
     HttpEntity responseEntity = resp.getEntity(); 
     String response = EntityUtils.toString(responseEntity); 
     Log.i("info1", response); 
     JSONObject result = new JSONObject(response); 
     JSONArray arr = result.getJSONArray("data"); 
     Log.i("info2", arr.toString()); 

只是爲了看看我是否能帶走任何東西,比方說,數組命名爲「數據」。然而logcat沒有顯示任何東西。它顯示了前2個日誌,但不是第3個日誌。有任何想法嗎?

+0

你爲什麼不使用Facebook的SDK?這樣的示例代碼將幾乎告訴你如何做到這一點 – Igy

+0

我確實使用它。但是你有什麼示例代碼? –

回答

1

看起來像JSONObject。你可以通過它循環,並用數據做任何你想做的事情。 查看http://json.org/獲取特定語言的文檔(我假設您需要java,所以點擊java鏈接)。

我追加的數據表給你一個想法(jQuery的/ JavaScript的):

fiddle:http://jsfiddle.net/H8LNB/4/

+0

謝謝,那當然有幫助! –