我如何在 Android 中解析这个 JSON? [英] How can I parse this JSON in Android?

查看:30
本文介绍了我如何在 Android 中解析这个 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拉出用户块.JSON 结果会一直变化,有时会返回 4 个用户,有时会返回 10 个等等.

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
    }

推荐答案

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

这篇关于我如何在 Android 中解析这个 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆