2010-08-30 43 views
0

我想拉出用户块。 JSON结果都是会改变的,有时4名的用户将被退回,有时10等我该如何解析Android中的这个JSON?

{ 
     "results": [ 
     { 
      "user": { 
      "avatar_url_thumb": "http://avatars.stocktwits.com/production/9998/thumb-1270014645.png?1270014645", 
      "avatar_url_medium": "http://avatars.stocktwits.com/production/9998/medium-1270014645.png?1270014645", 
      "created_at": "2010-03-15T05:44:51Z", 
      "following_count": 14, 
      "updated_at": "2010-08-30T18:22:15Z", 
      "id": 9998, 
      "updates_count": 31, 
      "avatar_url_large": "http://avatars.stocktwits.com/production/9998/large-1270014645.png?1270014645", 
      "investor_relations": false, 
      "last_name": "Reporter", 
      "followers_count": 25, 
      "recommended": false, 
      "bio": "Apple News & AAPL Stock Analysis, visit Apple Digest blog link above", 
      "login": "AppleReporter", 
      "first_name": "Apple" 
      } 
     }, 
     { 
      "user": { 
      "avatar_url_thumb": "http://api.stocktwits.com/images/default_avatar_thumb.jpg", 
      "avatar_url_medium": "http://api.stocktwits.com/images/default_avatar_medium.jpg", 
      "created_at": "2010-04-14T01:02:05Z", 
      "following_count": 0, 
      "updated_at": "2010-08-30T18:29:56Z", 
      "id": 12924, 
      "updates_count": 1, 
      "avatar_url_large": "http://api.stocktwits.com/images/default_avatar_large.jpg", 
      "investor_relations": false, 
      "last_name": "Shareholder", 
      "followers_count": 0, 
      "recommended": false, 
      "bio": null, 
      "login": "Imurphit", 
      "first_name": "Apple" 
      } 
     }, 
     { 
      "user": { 
      "avatar_url_thumb": "http://api.stocktwits.com/images/default_avatar_thumb.jpg", 
      "avatar_url_medium": "http://api.stocktwits.com/images/default_avatar_medium.jpg", 
      "created_at": "2010-04-17T20:52:09Z", 
      "following_count": 0, 
      "updated_at": "2010-08-30T18:31:23Z", 
      "id": 13234, 
      "updates_count": 0, 
      "avatar_url_large": "http://api.stocktwits.com/images/default_avatar_large.jpg", 
      "investor_relations": false, 
      "last_name": "Apple", 
      "followers_count": 0, 
      "recommended": false, 
      "bio": null, 
      "login": "apple11", 
      "first_name": "John" 
      } 
     }, 
     { 
      "user": { 
      "avatar_url_thumb": "http://api.stocktwits.com/images/default_avatar_thumb.jpg", 
      "avatar_url_medium": "http://api.stocktwits.com/images/default_avatar_medium.jpg", 
      "created_at": "2010-07-12T19:04:51Z", 
      "following_count": 0, 
      "updated_at": "2010-08-30T20:12:15Z", 
      "id": 18691, 
      "updates_count": 0, 
      "avatar_url_large": "http://api.stocktwits.com/images/default_avatar_large.jpg", 
      "investor_relations": false, 
      "last_name": "Smith", 
      "followers_count": 0, 
      "recommended": false, 
      "bio": null, 
      "login": "apple", 
      "first_name": "Jacob" 
      } 
     }, 
     { 
      "user": { 
      "avatar_url_thumb": "http://api.stocktwits.com/images/default_avatar_thumb.jpg", 
      "avatar_url_medium": "http://api.stocktwits.com/images/default_avatar_medium.jpg", 
      "created_at": "2010-07-13T17:06:27Z", 
      "following_count": 0, 
      "updated_at": "2010-08-30T20:12:30Z", 
      "id": 18808, 
      "updates_count": 3, 
      "avatar_url_large": "http://api.stocktwits.com/images/default_avatar_large.jpg", 
      "investor_relations": false, 
      "last_name": "apple", 
      "followers_count": 0, 
      "recommended": false, 
      "bio": null, 
      "login": "applejames", 
      "first_name": "James" 
      } 
     } 
     ], 
     "page": 1, 
     "symbol": false, 
     "per_page": 20, 
     "response": { 
     "status": 200 
     }, 
     "total_pages": 1, 
     "total_entries": 6 
    } 

回答

3

使用JSONObject

// Get some JSON from wherever 
String json = getJSONFromServer(); 

// Parse the JSON response into an object 
JSONObject object = new JSONObject(json); 

// Get the results array 
JSONArray users = object.getJSONArray("results"); 
for(int i = 0; i < users.length(); i++) { 
    // Each element in the results array is a JSONObject with a single 
    // property "user" which is a JSONObject that contains the user data 
    JSONObject user = users.getJSONObject(i).getJSONObject("user"); 

    // Do something with the user 
    String firstName = user.getString("first_name"); 
} 
+0

可以使用的“获取”功能的“可选”的版本,所以在如果用户对象可以改为'JSONObject user = users.getJSONObject(i).optJSONObject(“user”);'如果“user”不是一个属性,那么'user'变量将被设置为null抛出异常。 – joshperry 2010-08-31 03:45:47

+0

@Sheehan你也可以使用'keys()'和'has()'枚举或检查各种属性。 – joshperry 2010-08-31 03:46:46

+0

非常优雅的解决方案! – 2010-08-31 03:53:36