JSON解析在android中引发JSON异常

JSON解析在android中引发JSON异常,android,json,parsing,reddit,Android,Json,Parsing,Reddit,我正在尝试用Android解析JSON文档,以前我也可以使用相同的逻辑来解析。但在网站的这一部分,我的代码似乎被破坏了,我花了几个小时试图比较什么改变了,但我说不出来。我错过了什么 有人能告诉我为什么我的尝试失败了吗 目标JSON [ { "data" : { "after" : null, "before" : null, "children" : [ { "data" : { "approved_by" : null, "author" : "Bu

我正在尝试用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是一个
JSONArray
,其中“看似相同”的JSON是一个
JSONObject

我想如果您查看抛出的
JSONException
的堆栈跟踪,它会告诉您
JSONArray
不能被解析为
JSONObject

要分析它,您可以将代码更改为以下内容:

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

“selfText”项数据在
target JSON
部分被破坏(至少你已经发布了)。很抱歉,这是一个打字错误,我在我的附件中找到了它。我现在看到了额外的括号,哇,捕捉得好。你对如何调整代码有什么建议吗?只需将JSONObject类型更改为Array?那么您的意思是更改JSONObject data=response.getJSONObject(“data”);JSONArray hotTopics=data.getJSONArray(“子项”);同意你的建议吗?这对我不太管用。。你能澄清一下吗?我是说replaceJSONObject响应=newJSONObject(result);使用我提供的代码
JSONArray jArr = new JSONArray(result);
JSONObject response = jArr.getJSONObject(0); // grab the first object in the array