标签:code nbsp style 实现 ati str ring let 无法解析
直接上代码吧
1、JSONString转换为字典
/// JSONString转换为字典 /// /// - Parameter jsonString: <#jsonString description#> /// - Returns: <#return value description#> func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{ let jsonData:Data = jsonString.data(using: .utf8)! let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) if dict != nil { return dict as! NSDictionary } return NSDictionary() }
2、字典转换为JSONString
/** 字典转换为JSONString - parameter dictionary: 字典参数 - returns: JSONString */ func getJSONStringFromDictionary(dictionary:NSDictionary) -> NSString { if (!JSONSerialization.isValidJSONObject(dictionary)) { print("无法解析出JSONString") return "" } let data : NSData! = try? JSONSerialization.data(withJSONObject: dictionary, options: []) as NSData! let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue) return JSONString! }
Swift3 JSON字符串转字典和字典转JSON字符串的实现
标签:code nbsp style 实现 ati str ring let 无法解析
原文地址:http://www.cnblogs.com/yajunLi/p/6549859.html