您的位置:首页 > 移动开发 > IOS开发

IOS--JSON数据解析成字典

2015-08-10 20:21 435 查看

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


结果展示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: