Facebook个人资料信息JSON在Android中解析

时间:2014-02-11 18:10:47

标签: android json facebook

例如:如果下面给出的是json的人在facebook上的个人资料获得了通过Android应用程序登录facebook sdk,我们将如何获得学校名称从教育领域中的json数据在android 。请帮忙

数据:

  {
   "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"
}

2 个答案:

答案 0 :(得分: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");

                }                       


        }
    }

答案 1 :(得分: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");