使用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];
数据结果:
@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];
数据结果:
相关文章推荐
- Adidas大舌頭 jsoj qgxa cbdw
- jsonp跨域请求数据实例——手机号码查询
- Json字符串与字典对象的相互转化
- JSON详解
- 简单谈谈javascript中this的隐式绑定
- 收集-ExtJs使用总结
- JS设置/获取日期和时间
- 自己用js写的两个日历控件
- JavaScript 模拟 HashMap例子
- bzoj1016: [JSOI2008]最小生成树计数
- JavaScript奇技淫巧45招
- 把json串中“:”前面名称部分的双引号去掉,值中的双引号保留
- javascript实现一个简单的弹出窗
- bzoj1014: [JSOI2008]火星人prefix
- js函数总结
- 理解js中的自由变量以及作用域的进阶
- bzoj1013: [JSOI2008]球形空间产生器
- Python和JavaScript间代码转换的4个工具
- Js的Array数组对象详解
- JavaScript兼容问题汇总[实时更新]