Created
July 10, 2015 10:02
-
-
Save safecat/7246c75303778b26894b to your computer and use it in GitHub Desktop.
NSJSONSerialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| import UIKit | |
| // NSJSONSerialization(iOS5新增) 类似的第三方库有:SBJSON、TouchJSON、YAJL、JSONKit、NextiveJson。 | |
| // 可以将JSON数据转为Foundation对象,一般是 NSDictionary 或者 NSArray,或者将Foundation对象转换为JSON数据 | |
| // NSJSONReadingOptions | |
| // MutableContainers:转换的来的 NSArray 或 NSDictionary 是 mutable 的 | |
| // MutableLeaves:转换来的对象中,叶子节点的 NSString 是 mutable 的 | |
| // AllowFragments:可以转换形如 "string" 这样的字符串为 string 字符串 | |
| // 对象转JSON | |
| func obj2json(obj:AnyObject) -> String? { | |
| // if(!NSJSONSerialization.isValidJSONObject(obj)){ | |
| // print("无法转换") | |
| // } | |
| do{ | |
| let data : NSData! = try NSJSONSerialization.dataWithJSONObject(obj, options: NSJSONWritingOptions.PrettyPrinted) | |
| let str = NSString(data: data, encoding: NSUTF8StringEncoding) as String! | |
| return str | |
| }catch{ | |
| print("无法转换") | |
| return nil | |
| } | |
| } | |
| // JSON转对象 | |
| func json2obj(str:String) -> AnyObject? { | |
| let data : NSData! = str.dataUsingEncoding(NSUTF8StringEncoding) | |
| do{ | |
| let obj : AnyObject! = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) | |
| return obj | |
| }catch{ | |
| // print("无法转换") | |
| return nil | |
| } | |
| } | |
| func checkType(obj:AnyObject?) -> String { | |
| if(obj==nil){ | |
| return "nil" | |
| }else if(obj is NSDictionary){ | |
| return "NSDictionary" | |
| }else if(obj is NSArray){ | |
| return "NSArray" | |
| }else if(obj is String){ | |
| return "String" | |
| }else if(obj is Int){ | |
| return "Int" | |
| } | |
| return "not include" | |
| } | |
| let user = [ | |
| "name":"xxy", | |
| "tel":["mobile":"138", "home":"021"] | |
| ] | |
| let str : String! = obj2json(user) | |
| print(str) | |
| print(checkType(str)) | |
| var json : AnyObject! = json2obj(str) | |
| print("This is a "+checkType(json)+":") | |
| print(json) | |
| json = json2obj("[1,2,3,4]") | |
| print("This is a "+checkType(json)+":") | |
| print(json) | |
| json = json2obj("{\"a\":\"a\"}") | |
| print("This is a "+checkType(json)+":") | |
| print(json) | |
| json = json2obj("12345") | |
| print("This is a "+checkType(json)+":") | |
| print(json) | |
| // | |
| //// 检查类型 | |
| //var data = "[1,2,3]".dataUsingEncoding(NSUTF8StringEncoding) | |
| //do{ | |
| // var json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) | |
| // print(checkType(json)) | |
| //}catch{ | |
| // | |
| //} | |
| // | |
| // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment