2013-04-10 231 views
3

我正在與Facebook集成的Android應用程序。我正在使用fql查詢從Facebook獲取信息。我的fql方法是如何從Json對象獲取數據?

   void runfql(){ 
       String fqlQuery = "SELECT uid, name, pic_small,birthday FROM user WHERE uid IN " + 
        "(SELECT uid2 FROM friend WHERE uid1 = me())"; 
       Bundle params = new Bundle(); 
       params.putString("q", fqlQuery); 
       Session session = Session.getActiveSession(); 
       Request request = new Request(session, 
       "/fql",       
       params,       
       HttpMethod.GET,     
       new Request.Callback(){   
         public void onCompleted(Response response) { 

         JSONObject myjson=response.getGraphObject().getInnerJSONObject(); 
         Log.d("ResultResultResult: " ,""+myjson);       
         }     
        }); 
        Request.executeBatchAsync(request); 

       } 

myjson object has all info i want。像這樣

{"data":[{"uid":536089174,"birthday":"July 22","name":"Usman Aslam Sheikh"},{"uid":581379174,"birthday":"July 26","name":"Ammar Khalid"} 

問題是如何將這些信息存儲到不同的數組?

請正確使用此代碼。

+0

你找的JSONObject和JSONArray?或GSON也許? – BobTheBuilder 2013-04-10 06:57:55

回答

9
String jsonString = yourstring; 
JSONObject jsonResult = new JSONObject(jsonString); 
JSONArray data = jsonResult.getJSONArray("data"); 
if(data != null) { 
    String[] names = new String[data.length()]; 
    String[] birthdays = new String[data.length()]; 
    for(int i = 0 ; i < data.length() ; i++) { 
     birthdays[i] = data.getString("birthday"); 
     names[i] = data.getString("name"); 
    } 
} 

檢查http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

+0

這是什麼對象? '[{ 「ID」: 「1019」, 「水平」: 「1」, 「time_in_secs」: 「3」, 「標準桿」: 「0」, 「首字母」: 「EGB」, 「引用」:「失敗者!我是awesomer「,」time_stamp「:」2014-01-26 11:20:20「},{」id「:」1018「,」level「:」1「,」time_in_secs「:」3「,」 par「:」0「,」首字母縮寫「:」EGB「,」quote「:」你剛剛被一個女孩打敗!「,」time_stamp「:」2014-01-26 11:19:24「},{」 ID 「:」 1032" , 「水平」: 「1」, 「time_in_secs」: 「3」, 「平價」: 「0」, 「縮寫」: 「0」, 「報價」: 「輸家」,「TIME_STAMP 「:」2014-07-10 09:11:55「}]' – 2016-01-23 20:34:55

+0

String jsonString = yourstring; JSONObject jsonResult = new JSONObject(jsonString); JSONArray data = jsonResult.getJSONArray(「」);如果(data!= null){ String [] names = new String [data.length()]; String [] birthdays = new String [data.length()];對於(int i = 0; i 2017-12-10 20:14:49

0

編輯你的代碼是這樣的...這可能會幫助你..

JSONObject resultObject = new JSONObject(response); 

JSONArray JArray = resultObject.getJSONArray("data"); 

       for (int t=0; t<JArray.length(); t++) { 

        JSONObject JObject = JtArray.getJSONObject(t); 



builder.append(JObject.getString("uid")+": ");