ios5中apple增加了解析JSON的api——NSJSONSerialization。网上已经有人做过测试,NSJSONSerialization在效率上完胜SBJSON、TouchJSON、YAJL、JSONKit、NextiveJson。详情见这里。既然apple为我们提供了这么良好的工具,我们没理由不用吧。
NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。
下面提供一个从豆瓣电台下载的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 = [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数据View Code
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中apple增加了解析JSON的api——NSJSONSerialization。网上已经有人做过测试,NSJSONSerialization在效率上完胜SBJSON、TouchJSON、YAJL、JSONKit、NextiveJson。详情见这里。既然apple为我们提供了这么良好的...
NSJSONSerialization 实用探索
NSJSONSerialization ios5 JSON API
测试NSJSONSerialization,NSPropertyListSerialization,NSArchiver,NSKeyedArchiver 测试各种本地对象序列化API的应用 样本模型对象: { UUID = "90B471CC-C740-4C1E-A421-EA996E34B505"; point = "{...
替换为NSJSONSerialization。 Jay符合以下规范:JSON RFC4627 Open Swift C7 Jay Pure-Swift JSON解析器和格式化程序。 完全可流式输入和输出。 Linux和OS X就绪。 替换为NSJSONSerialization。 Jay符合以下规范:...
NSJSONTest 测试NSJSONSerialization中Value的类型是否和JSON中声明一致,即类型稳定性。 测试用例参见。
WebPImageSerialization WebPImageSerialization按照Foundation的NSJSONSerialization类的API约定在UIImage和图像之间进行编码和解码。 默认情况下, UIImage初始化程序无法解码GIF文件中的动画图像。 该库使用...
JSONKit-NSJSONSerialization-普遍认为效率不错的JSONKit,与苹果官方的JSONSerialization究竟差距有多大????##JSONKit##NSSerialization##效率比较代码(执行100*1000次查看内存消耗)(void)loadJSONKit {...
NSNUllCategory相信不少开发者,都被NSNull坑过,最常见的是服务器返回的json... 这个时候,NSJSONSerialization 会自动把他们换成 NSNull。当我们再去用dict[@“hello”]的时候,就会出触发exception,导致程序崩溃。
//把一个数组转换成JSON格式data=[NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];//把这个数组恢复回来array=[NSJSONSerialization JSONObjectWithData:data ...
在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式,通过本文给大家介绍iOS中json解析出现的null,nil,NSNumber的解决办法,感兴趣的朋友参考下
Objective-C Json 实例详解 通过使用NSJSONSerialization 可以Json与Foundation的相互转换。
Jay - 纯Swift的JSON解析器和格式化程序,可替代NSJSONSerialization
使用IOS5自带解析类NSJSONSerialization方法把数据转换为Json字符串以及解析Json字符串
适用于iOS的完整动画GIF支持,包括函数,NSJSONSerialization样式类和(可选)UIImage Swizzling
// 方法1:NSKeyedUnarchiver// 方法2:NSJSONSerialization// 方法1:NSKeyedArchiver// 方法2:N
使用NSJSONSerialization或者AFN框架的AFHTTPSessionManager(底层也是NSJSONSerialization)将NSData数据转化成OC对象,有时会出现URL正确,加载数据任然会报错: reason: '-[NSNull length]: unrecognized ...
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; NSLog(@"%@",dictionary); }; [post post:dic Url:@"xxxxxxx"];
- How to use NSJSONSerialization to transform JSON into Swift types, and why you should avoid using AnyObject whenever you can - The dangers of forced type casting and how to avoid them - Simple ...
从IOS5开始,APPLE提供了对json的原生支持(NSJSONSerialization),但是为了兼容以前的iOS版本,可以使用第三方库来解析Json。 本文将介绍TouchJson、 SBJson 、JSONKit 和 iOS5所支持的原生的json方法,解析国家...