Json解析
2017-07-11 本文已影响0人
小笨憨
1.项目原因获取到Socket后台传过来的JSon字符串
获取后为 Any 类型
let string = message as! String
let data = string.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
let jsonDic = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String : Any]
2.获取网络请求后的JSon
if JSONSerialization.isValidJSONObject(responseObject!) {
let jsonData = try? JSONSerialization.data(withJSONObject: responseObject!, options: JSONSerialization.WritingOptions.prettyPrinted)
let jsdonDic = try? JSONSerialization.jsonObject(with: jsonData!, options: JSONSerialization.ReadingOptions.mutableContainers)
success(jsdonDic as! Dictionary<String, Any>)
}