在Android

时间:2015-12-25 21:37:40

标签: java android json reddit

我正在尝试在Android上读取这个json字符串,但字段“thumbnail”总是返回空,即使它实际上从不为空,它应该返回url或“self”。

{
    "kind": "t3",
    "data": {
      "domain": "twitter.com",
      "banned_by": null,
      "media_embed": {},
      "subreddit": "nba",
      "id": "3y6vzw",
      "author": "Higgnkfe",
      "media": null,
      "score": 883,
      "approved_by": null,
      "over_18": false,
      "hidden": false,
      "num_comments": 811,
      "thumbnail": "http://b.thumbs.redditmedia.com/rfBhblr-DJIfsCEwQ9jq-wy_mHcmmedx3_Xy60STumc.jpg",
      "subreddit_id": "t5_2qo4s",
      "stickied": false,
      "from": null,
      "is_self": false,
      "from_id": null,
      "permalink": "/r/nba/comments/3y6vzw/first_all_star_results_are_in_kobe_steph_lead_the/",
      "created": 1451088664.0,
      "url": "https://twitter.com/nbaallstar/status/680417825463365632",
      "title": "First All Star Results are in; Kobe, Steph lead the vote",
      "created_utc": 1451059864.0,
      "ups": 883
    }  

这是我正在阅读的一段代码:

        String title, author, subreddit, id, thumbnail = "test", url;
        int score, numOfComments;
        double created;
        boolean isSelf;

        JSONArray arr = new JSONObject(raw).getJSONObject("data").getJSONArray("children");

        for (int i = 0; i < arr.length(); i++) {
            JSONObject data = arr.getJSONObject(i).getJSONObject("data");

            Log.d("LOADER", "JSON: " + data.toString());

            title = data.getString("title");
            author = data.getString("author");
            subreddit = data.getString("subreddit");
            id = data.getString("id");
            thumbnail = data.getString("thumbnail");
            score = data.getInt("score");
            Log.d("LOADER", " thumb: " + thumbnail); // log shows "thumb:" only
            url = data.getString("url");
            created = data.getDouble("created");
            isSelf = data.getBoolean("is_self");
            numOfComments = data.getInt("num_comments");

            Post post = new Post(subreddit, title, author, url, id, score, numOfComments,
                     thumbnail, created, isSelf);

            posts.add(post);
        }  

我在阅读任何其他字段时都没有问题,即使是“url”字段也包含网址,所以它不可能......

这是日志显示的data.toString():

JSON: {"domain":"self.nba","banned_by":null,.....(etc etc etc).......,"id":"3y46dt","num_comments":787,"thumbnail":"","subreddit_id":"t5_2qo4s","is_self":true,"from_id":null,"url":"https:\/\/www.reddit.com\/r\/nba\/comments\/3y46dt\/trash_talk_thread_no_games_today_because_of\/","author_flair_text":"[TOR] Luis Scola"....etc  

如您所见,“thumbnail”的值为“”,但“url”的确有一个网址。

这里发生了什么?

这是我正在获取json字符串的网址,以防它有用http://www.reddit.com/r/nba/.json

1 个答案:

答案 0 :(得分:2)

enter image description here我自己检查过,你的代码没有任何问题。事实上,您获得的json包含缩略图值。我已经转到您提供的链接,并且开始3个数组包含空值 缩略图。因此,您的代码是正确的,它与缩略图的值无关,无论它是网址还是其他值。