大佬教程收集整理的这篇文章主要介绍了NSJSONSerialization介绍,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
ios5中apple增加了解析JSON的api——NSJSONserialization。网上已经有人做过测试,NSJSONserialization在效率上完胜SBJSON、TouchJSONYAJLJSONKitNextiveJson。详情见这里。既然为我们提供了这么良好的工具,我们没理由不用吧。
NSJSONserialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。
s.width=650;" src="http://img.code.cc/vcimg/static/loading.png" src="webkit-fake-url://47353D67-93B2-4856-9993-222687911A92/ExpandedBlockStart.gif">
#define kGlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
#define kDoubanUrl @"http://douban.fm/j/mine/playlist?type=n&h=&chAnnel=0&from=mainsite&r=4941e23d79"
-(void) loadJsonData:(NSURL *)url
dispatch_async(kGlobalQueue,^{
NSData *data = [NSData dataWithContentsOfURL:url];
[self performSELEctorOnMainThread:@SELEctor(parseJsonData:) withObject:data waitUntilDone:NO];
});
}
-(void) parseJsonData:(NSData *)data
NSError *error;
NSDictionary *json = [NSJSONserialization JSONObjectWithData:data options:kNilOptions error:&error];
if (json == nil) {
NSLog(@"json parse Failed \r\n");
return;
}
NSArray *songArray = [json objectForKey:@"song"];
NSLog(@"song collection: %@\r\n",songArray);
_song = songArray;
self.songIndex = 0;
NSDictionary *song = [songArray objectATindex:0];
NSLog(@"song info: %@\t\n",song);
}
Foundation对象转换为json数据
s.width=650;" src="http://img.code.cc/vcimg/static/loading.png" src="webkit-fake-url://47353D67-93B2-4856-9993-222687911A92/ExpandedBlockStart.gif">
NSDictionary *song = [NSDictionary DictionaryWithObjectsAndKeys:@"i can fly",@"title",@"4012",@"length",@"Tom",@"Singer",nil];
if ([NSJSONserialization isValidJSONObject:song])
NSError *error;
NSData *jsonData = [NSJSONserialization dataWithJSONObject:song options:NSJSONWriTingPrettyPrinted error:&error];
NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json data:%@",json);
}
iOS5.0新功能之——NSJSONSerialization解析JSON数据
- (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]; }
以上是大佬教程为你收集整理的NSJSONSerialization介绍全部内容,希望文章能够帮你解决NSJSONSerialization介绍所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。