2014-02-11 76 views
1

例如:如果下面給出的人是在Facebook上的個人資料的json得到了thorugh Facebook的SDK登錄通過Android應用程序,我們將如何獲得學校名稱fron教育領域的JSON數據在android。請幫助Facebook的個人資料信息Android中的JSON解析

數據:

{ 
    "id": "1464730016", 
    "name": "Ravi Tamada", 
    "first_name": "Ravi", 
    "last_name": "Tamada", 
    "link": "https://www.facebook.com/ravi8x", 
    "username": "ravi8x", 
    "birthday": "12/22/1988", 
    "hometown": { 
     "id": "112158005464147", 
     "name": "Baruva" 
    }, 
    "location": { 
     "id": "102186159822587", 
     "name": "Chennai, Tamil Nadu" 
    }, 
    "bio": "Author: www.androidhive.info\r\nCo-author: www.9lessons.info", 
    "work": [ 
     { 
     "employer": { 
      "id": "179366562092719", 
      "name": "ByteAlly" 
     }, 
     "location": { 
      "id": "102186159822587", 
      "name": "Chennai, Tamil Nadu" 
     }, 
     "position": { 
      "id": "124917314217511", 
      "name": "Product Head" 
     } 
     ] 
     } 
    ], 
    "favorite_athletes": [ 
     { 
     "id": "18620649907", 
     "name": "Virat Kohli" 
     } 
    ], 
    "education": [ 
     { 
     "school": { 
      "id": "131587206873093", 
      "name": "Raghu Engineering College (REC)" 
     }, 
     "degree": { 
      "id": "140065339390579", 
      "name": "B.Tech" 
     }, 
     "year": { 
      "id": "142963519060927", 
      "name": "2010" 
     }, 
     "type": "Graduate School", 
     "classes": [ 
      { 
       "id": "192259410803415", 
       "name": "2010", 
       "with": [ 
        { 
        "id": "584960408", 
        "name": "Santosh Patnaik" 
        } 
       ], 
       "from": { 
        "id": "584960408", 
        "name": "Santosh Patnaik" 
       } 
      } 
     ] 
     } 
    ], 
    "gender": "male", 
    "relationship_status": "Single", 
    "website": "www.androidhive.info\nwww.9lessons.info\nwww.twitter.com/ravitamada\nwww.about.me/rv", 
    "timezone": 5.5, 
    "locale": "en_US", 
    "languages": [ 
     { 
     "id": "106059522759137", 
     "name": "English" 
     }, 
     { 
     "id": "107617475934611", 
     "name": "Telugu" 
     }, 
     { 
     "id": "112969428713061", 
     "name": "Hindi" 
     }, 
     { 
     "id": "343306413260", 
     "name": "Tamil" 
     } 
    ], 
    "verified": true, 
    "updated_time": "2012-03-02T17:04:18+0000" 
} 

回答

1
JSONObject jsonResult = new JSONObject(jsonUser); 
JSONArray data = jsonResult.getJSONArray("education"); 
if(data != null) 
    { 
    for(int i = 0 ; i < data.length() ; i++) 
     { 
      JSONObject c = data.getJSONObject(i); 
      String type = c.getString("type"); 
      if(type.equalsIgnoreCase("college")) 
       { 

        JSONObject school = c.getJSONObject("school"); 
        String id2 = school.getString("id"); 
        String name2 = school.getString("name"); 


        JSONObject year = c.getJSONObject("year"); 
        String id_y = school.getString("id"); 
        String name_y = school.getString("name"); 

       }      


     } 
    } 
0

假設您在這個JSON到您作爲檢索響應一個JSONObject,這是這​​樣的:

// Get jsonArray 'education' from main jsonObject 
JSONArray jsonArrayEducation = jsonObject.getJSONArray("education"); 
JSONObject jsonSchool = jsonArrayEducation.getJSONObject("school"); 

請注意,如果你有興趣只在名稱上,您可以將上述兩行分組爲

JSONObject jsonSchool = jsonObject.getJSONArray("education").getJSONObject("school"); 

// get school name 
String schoolName = jsonSchool.getString("name"); 
+0

「getJSONObject」函數不會將字符串作爲參數 –

相關問題