美文网首页
Json解析

Json解析

作者: 小笨憨 | 来源:发表于2017-07-11 15:50 被阅读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>)
     }
    

    相关文章

      网友评论

          本文标题:Json解析

          本文链接:https://www.haomeiwen.com/subject/tfdmhxtx.html