解析JSON
2013-10-18 17:22
211 查看
iOS5新加了很多新功能,解析JSON也是其中的一个。
下面是我最近写的一个小DMEO。关于JSON的,现放出与大家共享。
以下代码参考Vandad Nahavandipoor的 《iOS 5 Programming Cookbook》。
这个是保存的方法。
- (IBAction)touchWriteButton:(id)sender {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"Anthony" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
NSArray *arrayOfAnthonysChildren = [[NSArray alloc] initWithObjects:
@"Anthony's Son 1", @"Anthony's Daughter 1", @"Anthony's Son 2", @"Anthony's Son 3", @"Anthony's Daughter 2", nil];
[dictionary setValue:arrayOfAnthonysChildren forKey:@"children"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
NSLog(@"dic->%@",error);
}
[dictionary release];
BOOL succeed = [jsonData writeToFile:JSON_PATH atomically:YES];
if (succeed) {
NSLog(@"Save succeed");
}else {
NSLog(@"Save fail");
}
}
这个是读取的方法。
- (IBAction)touchReadButton:(id)sender {
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:JSON_PATH];
/* Now try to deserialize the JSON object into a dictionary */
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
if (jsonObject != nil && error == nil){
NSLog(@"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionary class]]){
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
} else if ([jsonObject isKindOfClass:[NSArray class]]){
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(@"Dersialized JSON Array = %@", deserializedArray);
} else {
NSLog(@"An error happened while deserializing the JSON data.");
}
}
[jsonData release];
}
此功能仅限于IOS5才能使用。他的解析效率是目前市面上最高的。iOS5系统API和5个开源库的JSON解析速度测试
XML和json都是用来保存、读取、传递数据。
下面是我最近写的一个小DMEO。关于JSON的,现放出与大家共享。
以下代码参考Vandad Nahavandipoor的 《iOS 5 Programming Cookbook》。
这个是保存的方法。
- (IBAction)touchWriteButton:(id)sender {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"Anthony" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
NSArray *arrayOfAnthonysChildren = [[NSArray alloc] initWithObjects:
@"Anthony's Son 1", @"Anthony's Daughter 1", @"Anthony's Son 2", @"Anthony's Son 3", @"Anthony's Daughter 2", nil];
[dictionary setValue:arrayOfAnthonysChildren forKey:@"children"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
NSLog(@"dic->%@",error);
}
[dictionary release];
BOOL succeed = [jsonData writeToFile:JSON_PATH atomically:YES];
if (succeed) {
NSLog(@"Save succeed");
}else {
NSLog(@"Save fail");
}
}
这个是读取的方法。
- (IBAction)touchReadButton:(id)sender {
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:JSON_PATH];
/* Now try to deserialize the JSON object into a dictionary */
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
if (jsonObject != nil && error == nil){
NSLog(@"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionary class]]){
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
} else if ([jsonObject isKindOfClass:[NSArray class]]){
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(@"Dersialized JSON Array = %@", deserializedArray);
} else {
NSLog(@"An error happened while deserializing the JSON data.");
}
}
[jsonData release];
}
此功能仅限于IOS5才能使用。他的解析效率是目前市面上最高的。iOS5系统API和5个开源库的JSON解析速度测试
XML和json都是用来保存、读取、传递数据。
相关文章推荐
- AS3解析JSON
- Delphi使用SuperObject解析Json时提示“Integer overflow”
- Java post请求发送json数据在filter中解析方法
- Java构造和解析Json数据的两种方法详解2
- Android学习笔记---26_采用JSON格式返回数据给资讯客户端,效率上要高于xml文件解析和传输
- Gson解析Json数据
- Java构造和解析Json数据的两种方法详解2
- iOS开发网络篇 一一 JSON解析
- AS3解析JSON(二)
- java封装json数组,js解析json数组
- js 将json字符串转换为json对象的方法解析
- listview滚动到底部可以继续加载,结合json解析
- json解析!
- java里面json格式解析
- Java下利用Jackson进行JSON解析和序列化
- 关于json格式字符串解析并用mybatis存入数据库
- ajax中解析json数据的各种方法(返回类型为json或text)
- ASP( VBScript ) 解析 JSON
- javaScript解析Json
- Android之解析Android Map地图返回的Json数据