在另一个数组中获取JSON数组

时间:2016-01-27 00:12:33

标签: java android json

我正在编写一个将新闻源下载到主页的应用程序。我正在使用JSON文件来存储我的所有数据。 我的问题是我无法从外部阵列“feed”中拉出数组“comments”。我甚至通过几个验证器运行我的JSON没有问题,但是在运行应用程序时,我得到这条消息:

System.err: org.json.JSONException: No value for comments

这是我的JSON:

{
  "feed": [
    {
      "id": 1,
      "name": "National Geographic Channel",
      "image": "http://api.androidhive.info/feed/img/cosmos.jpg",
      "status": "\"Science is a beautiful and emotional human endeavor,\" says Brannon Braga, executive producer and director. \"And Cosmos is all about making science an experience.\"",
      "profilePic": "http://api.androidhive.info/feed/img/nat.jpg",
      "timeStamp": "1403375851930",
      "url": null,
      "comments": [
        {
          "comment": {
            "id": 1,
            "content": "Great Post xD",
            "user": "SparkWings"
          }
        }
      ]
    },
    {
      "id": 2,
      "name": "TIME",
      "image": "http://api.androidhive.info/feed/img/time_best.jpg",
      "status": "30 years of Cirque du Soleil's best photos",
      "profilePic": "http://api.androidhive.info/feed/img/time.png",
      "timeStamp": "1403375851930",
      "url": "http://ti.me/1qW8MLB",
      "comments": [
        {
          "comment": {
            "id": 1,
            "content": "Great Post xD",
            "user": "SparkWings"
          }
        }
      ]
    },
    {
      "id": 3,
      "name": "Abraham Lincoln",
      "image": null,
      "status": "That some achieve great success, is proof to all that others can achieve it as well",
      "profilePic": "http://api.androidhive.info/feed/img/lincoln.jpg",
      "timeStamp": "1403375851930",
      "url": null,
      "comments": [
        {
          "comment": {
            "id": 1,
            "content": "Great Post xD",
            "user": "SparkWings"
          }
        }
      ]
    }
  ]
}

这是我试图用来获取所有数据的方法。

 private void parseJsonFeed(JSONObject response) {
    try {
        JSONArray feedArray = response.getJSONArray("feed");

        for (int i = 0; i < feedArray.length(); i++) {
            JSONObject feedObj = (JSONObject) feedArray.get(i);

            FeedItem item = new FeedItem();
            item.setId(feedObj.getInt("id"));
            item.setName(feedObj.getString("name"));

            // Image might be null sometimes
            String image = feedObj.isNull("image") ? null : feedObj
                    .getString("image");
            item.setImge(image);
            item.setStatus(feedObj.getString("status"));
            item.setProfilePic(feedObj.getString("profilePic"));
            item.setTimeStamp(feedObj.getString("timeStamp"));

            JSONArray comments = response.getJSONArray("comments");

            final ArrayList<String> comment = new ArrayList<>();

            for(int j = 0; j < comments.length(); j++)
            {
                JSONObject o = (JSONObject) comments.get(i);
                StringBuilder b = new StringBuilder();
                int commentId = comments.getJSONObject(i).getInt("id");
                String cont = comments.getJSONObject(i).optString("content");
                String user = comments.getJSONObject(i).optString("user");

                b.append(cont + " -");
                b.append(user);


                comment.add(b.toString());
            }

            item.setComments(comment);

            // url might be null sometimes
            String feedUrl = feedObj.isNull("url") ? null : feedObj
                    .getString("url");
            item.setUrl(feedUrl);

            feedItems.add(item);
        }

        // notify data changes to list adapater
        listAdapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

感谢任何和所有帮助。提前谢谢!

3 个答案:

答案 0 :(得分:0)

您正在尝试从顶级对象response获取“评论”。该对象只有一个字段:“feed”。您需要从feedObj获取“评论”。

feed
  feedobj[0]
    id
    name
    ...
    comments
      comment[0]
      ...
  ...

答案 1 :(得分:0)

看起来你在json的顶层要求一个名为“comments”的数组,这个数组不存在。顶级唯一的东西就是“饲料”。您需要从feedObj而不是response获取“评论”。

答案 2 :(得分:0)

使用response.getJSONArray()不适用于嵌套的JSON数组。 comments对象位于feed对象中。我的建议是feedObj.getJSONArray(“comments”)。