ios-day21-02(ios开发之json数据的解析(json的反序列化))

1,JSON中的对象反序列化为OC中的NSDictionary

2,JSON中的数组反序列化为OC中的NSArray


JSON的解析方式:

1,从iOS5开始,苹果提供了对json的原生支持,即使用NSJSONSerialization来解析json

2,可以使用第三方库来解析json,常见的解析json的第三方库有:SBJson、JSONKit、TouchJson等


反序列化:[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

序列化:[NSJSONSerialization dataWithJSONObject:array options:0 error:NULL];


下面这几段代码介绍了使用iOS5自带的NSJSONSerialization如何解析json数据(即如何对json进行反序列化),

至于如何使用第三方库来解析json,参见博客:http://blog.csdn.net/enuola/article/details/7903632


//
//  JLViewController.m
//  02-JSON
//
//  Created by XinYou on 15-4-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLViewController.h"
#import "JLPerson.h"
#import "JLAddress.h"

@interface JLViewController ()

@end

@implementation JLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self jsonToModel];
}

/**
 *  json字符串转模型(json反序列化)
 */
- (void)jsonToModel{

    /**
     将下面的json数据转成模型数据
     
      { "phone" : ["12345678", "87654321","02887188812"],
        "name" : "张三", 
        "age" : 26, 
        "address" : { "country" : "中国","province" : "湖南" }, 
        "married" : false }
     */
    
    // 需要对json中的双引号转义
    NSString *json = @"{ \"phone\" : [\"12345678\", \"87654321\",\"02887188812\"],\"name\" : \"张三\", \"age\" : 26, \"address\" : { \"country\" : \"中国\",\"province\" : \"湖南\" }, \"married\" : false }";
    
    // NSString转NSData
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *error;
    
    // json转字典
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    // error不为空,表示解析出错
    if (error != nil) {
        // 打印错误信息
        NSLog(@"%@", error.localizedDescription);
        
        // 直接返回
        return;
    }
    
    // 打印字典
    NSLog(@"%@", dict);
    /**
     打印结果如下:
     {
         address = {country = "\U4e2d\U56fd";province = "\U6e56\U5357";};
         age = 26;
         married = 0;
         name = "\U5f20\U4e09";
         phone = (12345678,87654321,02887188812);
     }
     */
    
    // 字典转模型
    JLPerson *person = [JLPerson personWithDict:dict];
    
    // 由上面的打印结果可知,address也是一个字典
    NSDictionary *addressDict = dict[@"address"];
    
    // 字典转模型
    JLAddress *address = [JLAddress addressWithDict:addressDict];
    
    person.address = address;
    
    // 打印person
    NSLog(@"%@", person);
    /**
     打印结果如下(重写了JLPerson和JLAddress的discription方法):
     
    
    
     
      
     {name : 张三, 
     age: 26, 
     married: 0, 
     phone : (12345678,87654321,02887188812), 
     address : 
     
     
      
       {country : 中国, province : 湖南}}
     */
}

@end

     
     
    
    
//
//  JLPerson.h
//  02-JSON
//
//  Created by XinYou on 15-4-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
    
    
     
     
@class JLAddress;

@interface JLPerson : NSObject
// 名字
@property (nonatomic, copy)NSString *name;
// 年龄
@property (nonatomic, strong)NSNumber *age;
// 是否结婚
@property (nonatomic, assign)BOOL married;
// 组数中存储的是所有电话号码
@property (nonatomic, strong)NSArray *phone;
// 地址
@property (nonatomic, strong)JLAddress *address;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)personWithDict:(NSDictionary *)dict;

@end
//
//  JLPerson.m
//  02-JSON
//
//  Created by XinYou on 15-4-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLPerson.h"

@implementation JLPerson

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}

+ (instancetype)personWithDict:(NSDictionary *)dict{

    return [[self alloc] initWithDict:dict];
}

- (NSString *)description{

    return [NSString stringWithFormat:@"<%@ : %p> {name : %@, age: %@, married: %d, phone : %@, address : %@}", [self class], self, self.name, self.age, self.married, self.phone, self.address];
}

@end
//
//  JLAddress.h
//  02-JSON
//
//  Created by XinYou on 15-4-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
     
     
      
      

@interface JLAddress : NSObject
// 国家
@property (nonatomic, copy)NSString *country;
// 省份
@property (nonatomic, copy)NSString *province;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)addressWithDict:(NSDictionary *)dict;

@end
//
//  JLAddress.m
//  02-JSON
//
//  Created by XinYou on 15-4-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLAddress.h"

@implementation JLAddress

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}

+ (instancetype)addressWithDict:(NSDictionary *)dict{

    return [[self alloc] initWithDict:dict];
}

- (NSString *)description{

    return [NSString stringWithFormat:@"<%@ : %p> {country : %@, province : %@}", [self class], self, self.country, self.province];
}

@end

     
     
    
    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值