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

NSJSONSerialization

发布时间:2020-12-16 19:49:46 所属栏目:百科 来源:网络整理
导读:json - object #definekGlobalQueuedispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0) #definekDoubanUrl@"http://douban.fm/j/mine/playlist?type=nh=channel=0from=mainsiter=4941e23d79" -( void )loadJsonData:(NSURL*)url { dispatch_asyn
json -> object
  1. #definekGlobalQueuedispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
  2. #definekDoubanUrl@"http://douban.fm/j/mine/playlist?type=n&h=&channel=0&from=mainsite&r=4941e23d79"
  3. -(void)loadJsonData:(NSURL*)url
  4. {
  5. dispatch_async(kGlobalQueue,^{
  6. NSData*data=[NSDatadataWithContentsOfURL:url];
  7. [selfperformSelectorOnMainThread:@selector(parseJsonData:)withObject:datawaitUntilDone:NO];
  8. });
  9. }
  10. -(void)parseJsonData:(NSData*)data
  11. {
  12. NSError*error;
  13. NSDictionary*json=[NSJSONSerializationJSONObjectWithData:dataoptions:kNilOptionserror:&error];
  14. if(json==nil){
  15. NSLog(@"jsonparsefailedrn");
  16. return;
  17. NSArray*songArray=[jsonobjectForKey:@"song"];
  18. NSLog(@"songcollection:%@rn",songArray);
  19. _song=songArray;
  20. self.songIndex=0;
  21. NSDictionary*song=[songArrayobjectAtIndex:0];
  22. NSLog(@"songinfo:%@tn",song);
  23. }

object -> JSON
  1. NSDictionary*song=[NSDictionarydictionaryWithObjectsAndKeys:@"icanfly",@"title",@"4012",@"length",@"Tom",@"Singer",nil];
  2. if([NSJSONSerializationisValidJSONObject:song])
  3. {
  4. NSError*error;
  5. NSData*jsonData=[NSJSONSerializationdataWithJSONObject:songoptions:NSJSONWritingPrettyPrintederror:&error];
  6. NSString*json=[[NSStringalloc]initWithData:jsonDataencoding:NSUTF8StringEncoding];
  7. NSLog(@"jsondata:%@",json);
  8. }

(编辑:李大同)

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

    推荐文章
      热点阅读