Jorge Gil
Jorge Gil

Reputation: 2865

Json value returns empty (when it's not) when parsing on Android

I'm trying to read this json string on Android but the field "thumbnail" always returns empty even though it never actually is empty, it should return either a url or "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
    }  

Here's a piece of code i'm using to read it:

        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);
        }  

I have no problem reading any other field, even the "url" field which also contains a url, so it can't be that...

This is what the log shows for 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  

As you can see, "thumbnail" has a value of "", but "url" does have a url.

What's going on here?

Here's the url where i'm getting the json string from in case it's useful http://www.reddit.com/r/nba/.json

Upvotes: 2

Views: 771

Answers (1)

Abhishek Singh
Abhishek Singh

Reputation: 295

enter image description hereI checked it myself, nothing is wrong with your code. The fact is the json which you are getting contains empty value for thumbnail. I had gone to the link which you provided and starting 3 arrays contains empty value for thumbnail. So your code is correct it has nothing to do with the value of thumbnail whether it's a url or some other value.

Upvotes: 2

Related Questions