一尘不染

如何在Android中解析此JSON?

json

我想拉出用户块。JSON结果将始终更改,有时将返回4个用户,有时将返回10个,等等。

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

阅读 190

收藏
2020-07-27

共1个答案

一尘不染

使用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");
 }
2020-07-27