温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

关于iOS开发 Json解析的几种方式

发布时间:2020-08-04 07:43:35 来源:网络 阅读:465 作者:冬季besos 栏目:移动开发

一、NSJSONSerialization

NSJSONSerialization是iOS 5.0新增的类,用来尽心对Json数据的解析。使用方法如下:

Json To Foundation

#define kGlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)

-(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;

}

//此处通过json字典变量来读取数据

}

Foundation To Json

- (void)convertToJson: (NSData *)data

{

if ([NSJSONSerialization isValidJSONObject:data])

{

NSError *error;


NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&error];

NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSLog(@"json data:%@",json);

}

}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI