在某些时候我们需要将一些数据解析为我们需要的格式(假设以下为我们的原始待解析的数据):
[
{
"number":"123",
"name":"于",
"sex":"女",
"phone":"8888"
},
{
"number":"2",
"name":"于",
"sex":"女",
"phone":"888"
},
{
"number":"3",
"name":"袁",
"sex":"男",
"phone":"999"
}
]
以下为具体的解析步骤:
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// 1.获得文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"studentJSON" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:path];
// 2.json解析
id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"%@", [object class]);
NSLog(@"%@", object);
[_window release];
return YES;
}
-(void)dealloc
{
[_window release];
[super dealloc];
}