解析JSON时我们遇到swift问题。如果您查看示例图像,您将看到我们的问题。我们从json获得字符串。当我们分配到dictionary2时,我们得到例外:
“无法将'_NSArrayM'类型的值(0x10652de00)转换为'NSDictionary'(0x10652e2d8)。”
我们如何解析这个?我们无法快速更新到最新版本。
let urlJsonToken2 = "https://jsonservices.mywebsite.com/api/en/News"
let myURL2 = NSURL(string: urlJsonToken2)
let request2 = NSMutableURLRequest(url: myURL2 as! URL)
request2.httpMethod = "GET"
request2.addValue("Bearer "+token!, forHTTPHeaderField: "Authorization")
request2.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
request2.setValue("application/json", forHTTPHeaderField: "Accept")
let task2 = URLSession.shared.dataTask(with: request2 as URLRequest) {(data2, response2, error2) -> Void in
if let unwrappedData2 = data2 {
do {
let requestReply = NSString(data: data2!, encoding: String.Encoding.ascii.rawValue)
let tokenDictionary2:NSDictionary = try JSONSerialization.jsonObject(with: unwrappedData2, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
let Name = tokenDictionary2["Name"] as? String
}
catch {}
}
}
task2.resume()
答案 0 :(得分:0)
该错误告诉您正在尝试将数组解析为字典。
BorderBrush
注意:使用Swift时,除非确实需要,否则应避免使用NS类。 Swift拥有自己的桥接类型。 NSDictionary = Dictionary,NSArray = Array