2015-12-25 46 views
2

我試圖讀取Android上的這個json字符串,但字段「縮略圖」總是返回空,即使它實際上沒有空,它應該返回一個url或「自我」。當在Android上解析時,Json值返回空(當它不是)

{ 
    "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」欄中也包含一個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 

正如你所看到的,「縮略圖」 H作爲「」的值,但「url」確實有一個url。

這是怎麼回事?

這裏就是我得到的JSON字符串在情況下,它是非常有用的http://www.reddit.com/r/nba/.json

回答

2

enter image description here我檢查自己的網址,沒有什麼是你的代碼錯誤。事實是你正在得到的json包含值爲縮略圖。我去了你提供的鏈接開始3個數組包含空值縮略圖。所以你的代碼是正確的,它與縮略圖的值無關,無論是url還是其他值。

+0

你使用什麼瀏覽器?當我用Chrome打開它時,它可能是「自我」或網址。我檢查了IE瀏覽器,但下載的文件顯示一個空值。我猜Android也一樣。 –

+0

@JorgeGil我使用了chrome,然後https://jsonformatter.curiousconcept.com/來格式化json以清楚地看到它。 –

+0

如果我使用Chrome瀏覽器並將輸出複製並粘貼到您發佈的網站上,我會獲得「thumbnail」的值,但是如果我只是直接將網址粘貼到網站上,我就沒有價值,我不知道發生了什麼... –

相關問題