NSJSONSerialization能够处理的JSONData
NSJSONSerialization能够处理的JSONData
You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.
你用NSJSONSerialization这个类来将JSON数据转换成Foundation对象或者将Foundation对象转换成JSON数据。
An object that may be converted to JSON must have the following properties:
一个能被转换成JSON数据的对象必须具备以下的特性:
- The top level object is an NSArray or NSDictionary.
- All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
- All dictionary keys are instances of NSString.
- Numbers are not NaN or infinity.
- 最顶层的对象必须是NSArray或者NSDictionary
- 所有的实例对象必须是NSString、NSNumber、NSArray、NSDictionary或者NSNull
- 所有字典中的键值必须是NSString
- Numbers必须是有意义的(不能是无穷大)
提供测试使用的源码:
NSDictionary+JSON.h 与 NSDictionary+JSON.m
// // NSDictionary+JSON.h // Category // // Created by YouXianMing on 14-8-28. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @interface NSDictionary (JSON) // 转换成JSONString - (NSString *)toJSONString; // 转换成JSONData - (NSData *)toJSONData; @end
// // NSDictionary+JSON.m // Category // // Created by YouXianMing on 14-8-28. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "NSDictionary+JSON.h" @implementation NSDictionary (JSON) - (NSString *)toJSONString { NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:nil]; if (data == nil) { return nil; } NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return string; } - (NSData *)toJSONData { NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:nil]; return data; } @end
NSData+JSON.h 与 NSData+JSON.m
// // NSData+JSON.h // Category // // Created by YouXianMing on 14-8-28. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @interface NSData (JSON) // 转换成集合 - (id)toPropertyList; @end
// // NSData+JSON.m // Category // // Created by YouXianMing on 14-8-28. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "NSData+JSON.h" @implementation NSData (JSON) - (id)toPropertyList { return [NSJSONSerialization JSONObjectWithData:self options:NSJSONReadingMutableLeaves error:nil]; } @end
使用时的源码:
// // ViewController.m // JSON // // Created by YouXianMing on 14-10-8. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 组织JSON数据 NSDictionary *jsonDic = \ @{ @"name" : @"YouXianMing", // NSString @"age" : @26, // NSNumber @"BWH" : @[@1, @1, @1], // NSArray @"address" : @{@"BeiJin": @"XXXX", @"XianNing":@"YYYY"}, // NSDictionary @"HasGirlFriend" : [NSNull null] // NSNull }; // 转换成JSONData NSData *jsonData = [jsonDic toJSONData]; // 将JSONData转换成list NSLog(@"%@", [jsonData toPropertyList]); } @end
以下是要点:
还有一点需要注意:
所有的数字相关的类型都会被转换成NSNumber,无论是布尔值还是浮点值还是整型值,都会被转换成NSNumber。