本文介绍了NSJSONSerialization上的Swift Dynamic Cast失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个,如下所示:

[ 
{ "Name" : "Ernst Handel", "City" : "Graz", "Country" : "Austria" },
{ "Name" : "Wolski Zajazd", "City" : "Warszawa", "Country" : "Poland" }
]

我将使用以下方法将其转换为NSDictionary:

and I am converting it to NSDictionary using:

var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(
 data,
 options: NSJSONReadingOptions.MutableContainers,
 error: nil)
as NSDictionary


推荐答案

我经常使用它。适用于JSON的数字和关联类型。它也适用于你。我试过了。

I usually use this. Works for both numeric and associative types of JSON. It works for your as well. I tried.

func JSONParseArray(jsonString: String) -> [AnyObject] {
if let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding) {
if let array = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil)  as? [AnyObject] {
    return array
}
}
return [AnyObject]()
}

这篇关于NSJSONSerialization上的Swift Dynamic Cast失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 00:55