Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在swift 4中解析json时出错_Json_Swift - Fatal编程技术网

在swift 4中解析json时出错

在swift 4中解析json时出错,json,swift,Json,Swift,我想在我的应用程序中使用JSON从网站获取数据。但问题是,当我试图在JSON数据中获取key[“posts”]的值时,会出现错误 let url = NSURL(string: "http://bongdavn.com/category/du-lieu/?json=1")! let request = NSMutableURLRequest(url: url as URL) URLSession.shared.dataTask(with: request as URLRequest) { (da

我想在我的应用程序中使用JSON从网站获取数据。但问题是,当我试图在JSON数据中获取key[“posts”]的值时,会出现错误

let url = NSURL(string: "http://bongdavn.com/category/du-lieu/?json=1")!
let request = NSMutableURLRequest(url: url as URL)
URLSession.shared.dataTask(with: request as URLRequest) { (data : Data?, urlResponse : URLResponse?, error : Error?) in
    if error != nil {
    }

    do {
        let json = try JSONSerialization.jsonObject(with: data!) as? [String : Any]
        let post = json!["post"] as! [String : Any]
        print(post)
        let content = post["content"] // error show from here.

        print(content)
        // the compiler show nil instead of value of "content"

        DispatchQueue.main.async {
            self.TableView.reloadData()
        }
    }
    catch {
        print("Catch the error : \(error)")
    }
}
.resume()
以下是我的json数据:

{  
   "status":"ok",
   "post":{  
      "id":121,
      "type":"post",
      "slug":"epl-england-premier-league-anh",
      "url":"http:\/\/bongdavn.com\/epl-england-premier-league-anh\/",
      "status":"publish",
      "title":"[EPL] England Premier League – Anh",
      "title_plain":"[EPL] England Premier League – Anh",
      "content":"<p>West Ham <strong><span class=\"hom\">1<\/span>\u00a0&#8211;\u00a0<\/strong><span class=\"awy\"><strong>1<\/strong>\u00a0<\/span>Leicester|||Crystal Palace\u00a0<strong><span class=\"hom\">2<\/span>\u00a0&#8211;\u00a0<\/strong><span class=\"awy\"><strong>1<\/strong>\u00a0<\/span>Stoke|||,
      "date":"2017-11-29 09:44:13",
      "modified":"2017-11-29 09:44:16",
      "categories":[  ],
      "tags":[  ],
      "author":{  },
      "comments":[  ],
      "attachments":[  ],
      "comment_count":0,
      "comment_status":"open",
      "custom_fields":{  }
   },
   "previous_url":"http:\/\/bongdavn.com\/bundesliga-dortmund-schalke\/"
}
{
“状态”:“确定”,
“职务”:{
“id”:121,
“类型”:“职位”,
“鼻涕虫”:“epl英格兰超级联赛anh”,
“url:“http:\/\/bongdavn.com\/epl英格兰超级联赛anh\/”,
“状态”:“发布”,
“头衔”:“[EPL]英格兰超级联赛&#Anh”,
“冠军平原”:“[EPL]英格兰超级联赛&8211;Anh”,
“内容”:“West Ham1\u00a0–;\u00a01\u00a0莱斯特| | |水晶宫\u00a02\u00a0–;\u00a01\u00a0斯托克| |”,
“日期”:“2017-11-29 09:44:13”,
“修改”:“2017-11-29 09:44:16”,
“类别”:[],
“标签”:[],
“作者”:{},
“评论”:[…],
“附件”:[],
“注释计数”:0,
“评论状态”:“打开”,
“自定义_字段”:{}
},
“上一个url”:“http:\/\/bongdavn.com\/德甲多特蒙德·沙尔克\/”
}
以下是错误:

无法将类型为“\uu NSSingleObjectArrayI”(0x109b9c328)的值强制转换为“NSDictionary”(0x109b9cf58)。 2017-11-29 22:45:19.764169+0700 BongDa[713:17684]无法将类型为“\uu NSSingleObjectArrayI”(0x109b9c328)的值强制转换为“NSDictionary”(0x109b9cf58)。 (lldb)


您发布的json错误,根据我从url获得的信息,有一个数组
posts
,因此您可以执行以下操作:

do {
    let json = try JSONSerialization.jsonObject(with: data!) as? [String : Any]
    let posts = json!["posts"] as! [[String: Any]] // posts is an array
    let post = posts.first! // get the first here, or according to your need
    print(post)
    let content = post["content"]
    print(content)

    DispatchQueue.main.async {
        self.TableView.reloadData()
    }
}
catch {
    print("Catch the error : \(error)")
}
}
.resume()

哪一行代码导致了错误?让content=post[“content”]从这里开始。请帮助我用
print(post)
的输出更新你的问题。但是OP声称这一行
let post=json![“post”]as![String:Any]
正在工作。@rmaddy在我的回答中,json是不同的。@Thánhtrunguyễn如果这样做有效,那么您的问题中的所有内容都是错误的。此代码与您发布的JSON不兼容。而此代码使用的JSON将导致您的代码在另一行崩溃。请正确更新您的问题,以便此答案对您的问题有意义。@maddy:谢谢您的评论。我会修复它。非常感谢