★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ )
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10717316.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
1、JSON字符串与字典(Dictionary)之间的相互转换
1 import Foundation 2 //JSON字符串转换为字典(Dictionary) 3 func getDictionaryFromJSONString(_ jsonString:String) ->NSDictionary{ 4 let jsonData:Data = jsonString.data(using: .utf8)! 5 let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) 6 if dict != nil { 7 return dict as! NSDictionary 8 } 9 return NSDictionary() 10 } 11 12 //字典(Dictionary)转换为JSON字符串 13 func getJSONStringFromDictionary(_ dictionary:NSDictionary) -> String { 14 if (!JSONSerialization.isValidJSONObject(dictionary)) { 15 print("无法解析出JSON字符串") 16 return String() 17 } 18 let data : NSData! = try? JSONSerialization.data(withJSONObject: dictionary, options: []) as NSData? 19 let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue) 20 return JSONString! as String 21 }
测试:
1 //JSON字符串 2 let str:String = "{"server":"192.168.0.1","port":"88","username":"administrator","password":"123456"}" 3 print(str) 4 //Print {"server":"192.168.0.1","port":"88","username":"administrator","password":"123456"} 5 6 //JSON字符串转换为字典(Dictionary) 7 let strDic = getDictionaryFromJSONString(str) 8 print(strDic) 9 //Print 10 /* 11 { 12 server = "192.168.0.1"; 13 port = 88; 14 username = administrator; 15 password = "123456"; 16 } 17 */ 18 19 //字典(Dictionary)转换为JSON字符串 20 let strJson = getJSONStringFromDictionary(strDic) 21 print(strJson) 22 //Print {"server":"192.168.0.1","port":"88","username":"administrator","password":"123456"}
2、JSON字符串与数组(Array)之间的相互转换
1 import Foundation 2 //JSON字符串转换为数组(Array) 3 func getArrayFromJSONString(_ jsonString:String) ->NSArray{ 4 let jsonData:Data = jsonString.data(using: .utf8)! 5 let array = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) 6 if array != nil { 7 return array as! NSArray 8 } 9 return array as! NSArray 10 } 11 12 //数组(Array)转换为JSON字符串 13 func getJSONStringFromArray(_ array:NSArray) -> String { 14 if (!JSONSerialization.isValidJSONObject(array)) { 15 print("无法解析出JSON字符串") 16 return String() 17 } 18 let data : NSData! = try? JSONSerialization.data(withJSONObject: array, options: []) as NSData! 19 let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue) 20 return JSONString! as String 21 }
测试:
1 //JSON字符串 2 3 let str:String = "{"employees":[{"firstName":"John","lastName":"Doe" },{ "firstName":"Anna","lastName":"Smith"}]}" 4 print(str) 5 //Print {"employees":[{"firstName":"John","lastName":"Doe" },{ "firstName":"Anna","lastName":"Smith"}]} 6 7 //JSON字符串转换为数组(Array) 8 let strArr = getArrayFromJSONString(str) 9 print(strArr) 10 //Print Could not cast value of type 'Foundation.NSDictionary' (0x7f7b01c80bb8) to 'Foundation.NSArray' (0x7f7b01c7ad60). 11 12 //数组(Array)转换为JSON字符串 13 let strJson = getJSONStringFromArray(strArr) 14 print(strJson) 15 //Print