SwiftyJSON中dict转JSON
do {
let dict = [
"isOwner":false,
"nickname":"Jackie",
"sn":"101190112014",
] as [String : Any]
let data = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
let json = JSON(data: data)
}catch let error {
DLog(error)
}
String转NSDictionary
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()
}
NSDictionary转String
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!
}