加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

NSJSONSerialization

发布时间:2020-12-16 19:50:15 所属栏目:百科 来源:网络整理
导读:自从iOS5.0以后,苹果推出了SDK自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也是比其他第三方开源项目的高很多,效率测试见后面。 NSJSONSerialization主要的方法包括NSDictionary、NSArray与json格式的转换,判断一

自从iOS5.0以后,苹果推出了SDK自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也是比其他第三方开源项目的高很多,效率测试见后面。

NSJSONSerialization主要的方法包括NSDictionary、NSArray与json格式的转换,判断一个data是不是json。通过豆瓣一段代码来看:

#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;
 //将json转化成NSArray、NSDictionary
 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; if (json == nil) { NSLog(@"json parse failed rn"); return; } NSArray *songArray = [json objectForKey:song]; NSLog(song collection: %@rn,songArray); _song = songArray; self.songIndex = 0; NSDictionary *song = [songArray objectAtIndex:song info: %@tn 
//通过将data转化为json
NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:i can fly",title4012lengthTomSingerif ([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解析器和第三方的库的解析速度对比见:http://www.52php.cn/article/p-pcmuoxxc-rq.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读