2016-05-03 101 views
-2
{Response: responseCode: 200, graphObject: {"id":"108303252910226","first_name":"Chris","last_name":"Jack","email":"[email protected]","gender":"male","birthday":"08\/08\/1984","hometown":{"id":"106377336067638","name":"Bangalore, India"}}, error: null} 

我从facebook获取此响应。 如何获得头名Android从Facebook获取数据json

我已经厌倦

try { 
           String res = response+""; 
           JSONObject j = new JSONObject(res); 
           String graphResponse = j.optString("graphObject"); 

           JSONObject j2 = new JSONObject(graphResponse); 

           String first_name = j2.getString("first_name"); 

           Log.i("firstname", ""+first_name); 
          } 
          catch (JSONException e) 
          { 
           e.printStackTrace(); 
          } 
+1

你到目前为止做了什么?发布代码 –

+0

了解json对象以及如何使用它,http://developer.android.com/intl/zh-tw/reference/org/json/JSONObject.html。 –

+0

这可能对您有所帮助http://stackoverflow.com/questions/30317540/how-do-i-fetch-name-and-email-using-facebook-sdk –

回答

0

您GraphRequest方法

GraphRequest request = GraphRequest.newMeRequest(
           loginResult.getAccessToken(), 
           new GraphRequest.GraphJSONObjectCallback() { 
            @Override 
    public void onCompleted(JSONObject object,GraphResponse response) { 
     object.getString("first_name") 
    } 
}); 
0

它是多维数组,所以你先洒数组里面。

String res="["+"{responseCode: 200, graphObject: {"id":"108303252910226","first_name":"Chris","last_name":"Jack","email":"[email protected]","gender":"male","birthday":"08\/08\/1984","hometown":{"id":"106377336067638","name":"Bangalore, India"}}, error: null}"+"]"; 
      JSONObject c; 
      JSONArray data=null; 
      try { 
       data = new JSONArray(res); 
      } catch (JSONException e1) { 
       e1.printStackTrace(); 
      } 
     try { 
     if(data != null) { 
       for (int i = 0; i < data.length(); i++) { 
        c = data.getJSONObject(i); 
        String lat=c.getString("responseCode"); 
        JSONArray dd=  c.getJSONArray("graphObject") 
         } 

然后json用dd字符串编码。