我想我的JSON响应转换成数组,但它不工作,我得到了我说的阵列中的项目是零转换JSON数据阵列中swift3
func getcomments(){
RestApiManager.sharedInstance.getComments(TUTORIAL_ID: id){
response in
let comments = JSON(response)
for item in comments.array!{
let comment = Comment(memail: String(describing: item["email"]), mcomment: String(describing: item["comment"]), mcomment_date: String(describing: item["comment_date"]), manswer: String(describing: item["answer"]), manswer_date: String(describing: item["answer_date"]))
self.comments.append(comment)
}
}
}
这是一个错误我的JSON响应:
[{
"email": "-",
"comment": "\u0627\u0632 \u0627\u067e\u0644\u06cc\u06a9\u06cc\u0634\u0646 \u062e\u0648\u0628\u062a\u0648\u0646 \u0645\u0645\u0646\u0648\u0646\u0645",
"comment_date": "2017-07-15 19:30:00",
"answer": null,
"answer_date": null
},
{
"email": "[email protected]",
"comment": "salam",
"comment_date": "2017-07-11 19:30:00",
"answer": "\u062a\u0634\u06a9\u0631",
"answer_date": "2017-07-12 03:50:57"
}
]
我在这一行了零误差:
意外发现零而展开的可选值
for item in comments.array!
就在这里'comments.array!'如果你要爆炸的东西,你最好确定它不是零。我认为你不能在这里保证。显然不是,因为它坠毁了。 – Abizern
“响应”的类型是什么?如果它是一个字符串,你需要使用'JSON(parseJSON :)' – Sweeper
@sweeper是我需要JSON(parseJSON :)它的工作原理谢谢 – fazed