您的位置:首页 > Web前端 > JavaScript

使用Json字符串对model对象赋值

2016-02-22 10:47 627 查看
User对象:

     @interface User : NSObject

     @property (nonatomic,
strong) NSString *name;

     @property (nonatomic,
strong) NSNumber *age;

     @end

使用Json字符串对model对象赋值:
    User *user = [[User
alloc]init];

    NSString *jsonStringSource =
@"{\"name\":\"张三\",\"age\":\"10\"}";
   
NSData *jsonDataSource = [jsonStringSource
dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *jsonObj = [NSJSONSerialization
JSONObjectWithData:jsonDataSource options:NSJSONReadingAllowFragments
error:nil];

    [user setValuesForKeysWithDictionary:jsonObj];

数据结果:



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