IOS开发--第二阶段--通讯--JSON系统解析

JSON系统解析:

1,创建请求路径和URL;

2,根据URL构建请求 ,如果是POST 使用NSMutableURLRequest , NS可变的URL请求;

NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:url];

3,构建连接 1、同步 2、异步;

NSData *responseData = [NSURLConnection sendSynchronousRequest:mutableURLRequest returningResponse:nil error:nil];

4,将data转化为字符串 查看数据的正确性;

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"responseString___%@", responseString);

5,使用系统类解析 1、要解析的数据 2、选项设置 3、错误信息;

    NSError *error = nil;

    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseData  options:NSJSONReadingMutableContainers error:&error];

    NSLog(@"dictionary____%@", dictionary);

    NSLog(@"%d_______%@", error.code, error.localizedDescription);

原文地址:https://www.cnblogs.com/gegeboke/p/4368493.html