2014-07-09 33 views
-2

我想解析Android中的JSON文檔,並且之前已經能夠使用相同的邏輯來完成此操作。但在網站的這一部分,我的代碼似乎打破了,我一直花費數小時試圖比較改變了什麼,但我不能說。我在這裏錯過了什麼?JSON解析在Android中引發JSON異常

有人可以教我爲什麼我的嘗試失敗?

目標JSON

[ { "data" : { "after" : null, 
    "before" : null, 
    "children" : [ { "data" : { "approved_by" : null, 
       "author" : "BuzzAldrinHere", 
       "author_flair_css_class" : null, 
       "author_flair_text" : null, 
       "banned_by" : null, 
       "clicked" : false, 
       "created" : 1404871393.0, 
       "created_utc" : 1404842593.0, 
       "distinguished" : null, 
       "domain" : "self.IAmA", 
       "downs" : 0, 
       "edited" : 1404849572.0, 
       "gilded" : 0, 
       "hidden" : false, 
       "id" : "2a5vg8", 
       "is_self" : true, 
       "likes" : null, 
       "link_flair_css_class" : "science", 
       "link_flair_text" : "", 
       "media" : null, 
       "media_embed" : { }, 
       "name" : "t3_2a5vg8", 
       "num_comments" : 7331, 
       "num_reports" : null, 
       "over_18" : false, 
       "permalink" : "/r/IAmA/comments/2a5vg8/i_am_buzz_aldrin_engineer_american_astronaut_and/", 
       "saved" : false, 
       "score" : 4968, 
       "secure_media" : null, 
       "secure_media_embed" : { }, 
       "selftext" : "I am hopi... 

守則嘗試

JSONObject response = new JSONObject(result); 
     JSONObject data = response.getJSONObject("data"); 
     JSONArray hotTopics = data.getJSONArray("children"); 
     for(int i=0; i<hotTopics.length(); i++) { 
      JSONObject topic = hotTopics.getJSONObject(i).getJSONObject("data"); 
      DetailsData item = new DetailsData(); 
      item.setAuthor(topic.getString("author")); 

      item.setPostTime(topic.getLong("created_utc")); 
      item.setScore(topic.getString("score")); 
      item.setComment(topic.getString("selftext")); 

看似相同的JSON,它實際上工作了

{ "data" : { "after" : "t3_2a8ij0", 
    "before" : null, 
    "children" : [ { "data" : { "approved_by" : null, 
      "author" : "JiveMonkey", 
      "author_flair_css_class" : null, 
      "author_flair_text" : null, 
      "banned_by" : null, 
      "clicked" : false, 
      "created" : 1404948237.0, 
      "created_utc" : 1404919437.0, 
      "distinguished" : null, 
      "domain" : "i.imgur.com", 
      "downs" : 0, 
      "edited" : false, 
      "gilded" : 0, 
      "hidden" : false, 
      "id" : "2a8tuu", 
      "is_self" : false, 
      "likes" : null, 
      "link_flair_css_class" : null, 
      "link_flair_text" : null, 
      "media" : null, 
      "media_embed" : { }, 
      "name" : "t3_2a8tuu", 
      "num_comments" : 1932, 
      "num_reports" : null, 
      "over_18" : false, 
      "permalink" : "/r/pics/comments/2a8tuu/norway_has_invented_a_bicycle_escalator/", 
      "saved" : false, 
      "score" : 4894, 
      "secure_media" : null, 
      "secure_media_embed" : { }, 
      "selftext" : "", 
      "selftext_html" : null, 
      "stickied" : false, 
      "subreddit" : "pics", 
      "subreddit_id" : "t5_2qh0u", 
      "thumbnail" : "http://a.thumbs.redditmedia.com/nIstmQ8l1jl-UiU8.jpg", 
      "title" : "Norway has invented a bicycle escalator", 
      "ups" : 4894, 
      "url" : "http://i.imgur.com/ACr2e4h.jpg", 
      "visited" : false 
      }, 
     "kind" : "t3" 
     }, 
+0

的「selfText」項目的數據在'目標JSON'部分細分(至少你已經張貼了什麼)。 – AedonEtLIRA

+0

對不起,這是一個錯字,我正確的在我的應用程序 – feilong

回答

0

目標JSON是JSONArray,其中的「看似相同的」 JSON是JSONObject

我試想一下,如果你看着堆棧跟蹤拋出的JSONException,它會告訴你,JSONArray不能被解析爲JSONObject

要分析它,你可以將代碼更改爲以下:

JSONArray jArr = new JSONArray(result); 
JSONObject response = jArr.getJSONObject(0); // grab the first object in the array 
+0

哦,我現在看到額外的支架,哇很好的捕獲。你有關於如何調整代碼的建議嗎?只需將JSONObject類型更改爲Array? – feilong

+0

所以你說改變JSONObject的數據= response.getJSONObject(「數據」); JSONArray hotTopics = data.getJSONArray(「children」);你的建議?這對我來說不太合適,你能澄清嗎? – feilong

+0

我是說替換JSONOBject響應=新的JSONObject(結果);用我提供的代碼 – panini