【问题标题】:Error in reading Json response读取 Json 响应时出错
【发布时间】:2015-12-06 17:07:08
【问题描述】:

我正在尝试从我的 json 提要中读取 json 响应,但日志猫向我显示此错误..不知道该怎么做...请帮助..

/Error ->: org.json.JSONException: Value
of type java.lang.String cannot be convert to JSONObject

我来自链接的 json 响应在这里...

{  
 "feed":[  
  {  
     "id":65,
     "name":"Hissam",
     "image":"http://deejay.org/feed/ironman.jpg",
     "status":"Hello thi is hissam",
     "profilePic":"http://deejay.org/feed/profilepic.jpg",
     "timeStamp":"1403375851930",
     "url":"http://google.com"
  },
  {  
     "id":65,
     "name":"Hissam",
     "image":"http://deejay.org/feed/cosmos.jpg",
     "status":"Hello thi is hissam",
     "profilePic":"http://deejay.org/feed/profilepic.jpg",
     "timeStamp":"1403375851930",
     "url":"http://google.com"
  }
 ]
}

这是我的 JSON 代码

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

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

            feedItems.add(item);
        }

【问题讨论】:

  • 向我们展示 Java 代码
  • 请贴出引发异常的代码。
  • 向我们提供 json 链接,我认为您的 json 已更新。
  • 您正在正确解析 json。请确保您将正确的 JSONObject 传递给您的 parseJsonFeed() 方法。
  • @Rah 我正在使用本地服务器进行测试.. 我没有在线链接可以分享

标签: java android arrays json


【解决方案1】:

通过添加解决了问题

  header('Content-type:application/json');

到我的 PHP 脚本。直到现在还不知道标题有那么重要。

【讨论】:

    猜你喜欢
    • 2015-12-03
    • 2013-03-11
    • 2021-06-17
    • 2012-09-16
    • 2015-08-12
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多