IOS--JSON数据解析成字典

JSON解析成字典

  • {} –>字典

  • [] –>数组

  • “”–>字符串

  • 11/11.1–>NSNumber

  • true/false –>NSNumber

  • null–>NSNull(注意:这也是一个对象)

转换流程

  • 1.创建URL

  • 2.依据URL创建请求

  • 3.利用NSURLConnection发送请求

  • 4.解析

代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   //json转dict
    //创建连接,要请求的jSON数据
    NSURL *url = [NSURL URLWithString:@"http://122.22.222.122:32812/login?

username=sky5156&pwd=sky5156&type=JSON"]; //创建请求 NSURLRequest *request = [NSURLRequest requestWithURL:url]; //发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { /** * 重要:事实上就是拿到data使用以下方法就能转成字典 *第一个參数:data 就是要转换的内容 *第二个參数:options是枚举值 NSJSONReadingMutableContainers(规则的可变数组或者字典), NSJSONReadingMutableLeaves (解析出可变字符串.这个有问题,不用) NSJSONReadingAllowFragments (非规则的字典或数组用这个) * */ NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",dict); }]; } @end

  • 结果展示
    这里写图片描写叙述
原文地址:https://www.cnblogs.com/mfmdaoyou/p/7149189.html