Could not cast value of type '__NSArrayM' to 'NSDictionary'

让人想犯罪 __ 提交于 2019-12-02 19:02:07

问题


I have a json.I am trying to parse that with that code.But its says

Could not cast value of type '__NSArrayM' to 'NSDictionary'

do {
    let dataDictionary: NSDictionary = try NSJSONSerialization.JSONObjectWithData(responseObject as! NSData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary // <------ Error

    if let customerArray = dataDictionary.valueForKey("cart") as? NSArray {
        for js in customerArray {
            let nameArray = js.valueForKey("name")
            let idArray = js.valueForKey("id")
        }
    }
}

Thank you for your helps


回答1:


The root object in your data is an array, not a object (dictionary).

You need to dynamically decide how to handle your JSON depending on the deserialized object.




回答2:


What it's telling you is that the JSON object that you're parsing is not a dictionary, it's an array. So if you change it so that you treat its value as an array instead of a dictionary, you'll be able to iterate over that.

You need to reevaluate your JSON to ensure that it's structured the way you think it is. It would also be useful if you posted the JSON that you're trying to parse so that we can see it's structure as well.



来源:https://stackoverflow.com/questions/33573508/could-not-cast-value-of-type-nsarraym-to-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!