大佬教程收集整理的这篇文章主要介绍了NSJSONSerialization介绍,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
ios5中apple增加了解析JSON的api——NSJSON@R_944_9464@lization。网上已经有人做过测试,NSJSON@R_944_9464@lization在效率上完胜SBJSON、TouchJSON、YAJL、JSONKit、NextiveJson。详情见这里。既然apple为我们提供了这么良好的工具,我们没理由不用吧。
NSJSON@R_944_9464@lization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。
View Code #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 = [NSJSON@R_944_9464@lization 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数据
View Code NSDictionary *song = [NSDictionary DictionaryWithObjectsAndKeys:@"i can fly",@"title",@"4012",@"length",@"Tom",@"Singer",nil]; if ([NSJSON@R_944_9464@lization isValidJSONObject:song]) { NSError *error; NSData *jsonData = [NSJSON@R_944_9464@lization dataWithJSONObject:song options:NSJSONWriTingPrettyPrinted error:&error]; NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"json data:%@",json); }
以上是大佬教程为你收集整理的NSJSONSerialization介绍全部内容,希望文章能够帮你解决NSJSONSerialization介绍所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。