views:

61

answers:

1

I want to pull out the user block. The JSON result will always change, sometimes 4 users will be returned, sometimes 10 etc.

 {
      "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
    }
+1  A: 

Use the 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");
 }
joshperry
You can use the "optional" version of the "get" functions so the line that gets the user object could be changed to `JSONObject user = users.getJSONObject(i).optJSONObject("user");` If "user" wasn't a property then the `user` variable would be set to null instead of throwing an exception.
joshperry
@Sheehan You can also use `keys()` and `has()` to enumerate or check for various properties.
joshperry
very elegant solution!
Sheehan Alam