美文网首页
swift 3.0 字典与JSON互转

swift 3.0 字典与JSON互转

作者: 李大围 | 来源:发表于2017-02-23 14:02 被阅读0次
    class JsonToSturt: NSObject {
    
    class func dictionaryWithJsonString(jsonString:String) ->NSDictionary {
        var dicx:NSDictionary!
        let data : Data! = jsonString.data(using: .utf8)
        let dic : Any! = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
        if dic != nil {
            dicx = dic as! NSDictionary
        }
        
        return dicx
    }
    class func toJSONS(representation:Any) -> String? {
        
        
        guard JSONSerialization.isValidJSONObject(representation) else {
            return nil
        }
        
        do {
            let data = try JSONSerialization.data(withJSONObject: representation, options: [])
            return String(data: data, encoding: .utf8)
        } catch {
            return nil
        }
    }

    相关文章

      网友评论

          本文标题:swift 3.0 字典与JSON互转

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