MJExtension(JSON到数据模型的自动转换)
整理自:http://www.jianshu.com/p/93c242452b9b。
1.MJExtension的功能
字典-->模型
模型-->字典
字典数组-->模型数组
模型数组-->字典数组
2.pod导入语句 pod 'MJExtension'
3.简单的数据模型转换
Student * stu = [Student mj_objectWithKeyValues:dict];
对象的属性名字要与json中的数据一致
如果有模型中嵌套模型也是直接用这句话,访问的时候用.来访问
eg:status.retweetedStatus.user.name
4.模型中有数组属性,数组里面装其他模型
在模型StatusResult内部实现
+(NSDictionary *)objectClassInArray{
return @{
@"statuses":@"Status",
@"ads":@"Ad"
}
}
StatusResult * result = [StatusResult mj_objectWithKeyValues:dict];
这种写法不需要导入Status和Ad的头文件,冒号后面写的就是类的名字
for(Status *status in result.statuses)遍历访问
5.如果模型中的属性的名字和字典的key不同(还有多级映射)
@interface Student : NSObject
@property (copy, nonatomic) NSString *ID;
@property (copy, nonatomic) NSString *desc;
@property (copy, nonatomic) NSString *nowName;
@property (copy, nonatomic) NSString *oldName;
@property (copy, nonatomic) NSString *nameChangedTime;
@property (strong, nonatomic) Bag *bag;
@end
@interface Bag : NSObject
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) double price;
@end
NSDictionary *dict = @{
@"id" : @"20",
@"desciption" : @"孩子",
@"name" : @{
@"newName" : @"lufy",
@"oldName" : @"kitty",
@"info" : @{
@"nameChangedTime" : @"2013-08"
}
},
@"other" : @{
@"bag" : @{
@"name" : @"小书包",
@"price" : @100.7
}
}
};
在Student中实现下面方法
+(NSDictionary *)mj_replacedKeyFromPropertyName{
return @{
@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info.nameChangedTime",
@"bag" : @"other.bag"
};
}
多级映射,前面是自定义的名字,后面是字典中的映射关系
6.将一个字典数组转成模型数组
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png",
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png",
}
];
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
7.将一个模型转为字典
NSDictionary *statusDict = status.keyValues;
8.将一个模型数组转成字典数组
NSArray *userArray = @[user1, user2];
NSArray *dictArray = [User keyValuesArrayWithObjectArray:userArray];
NSLog(@"%@", dictArray);
MJExtension(JSON到数据模型的自动转换)的更多相关文章
- IE8中JSON.stringify方法对自动转换unicode字符的解决方案
IE8内置了JSON对象,用以处理JSON数据.与标准方法的不同,IE8的JSON.stringify会把utf-8字符转码: var str = "我是程序员" var json ...
- springmvc 支持对象与json 自动转换的配置
基于maven的工程, 需要在pom.xml中添加如下依赖 <dependency> <groupId>javax.servlet</groupId> <ar ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析]
目录 前言 现象 源码分析 实例讲解 关于配置 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.c ...
- SpringMVC关于json、xml自动转换的原理研究
SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析 --转
SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 原文地址:http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-c ...
- JSON字符串自动转换
package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObj ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析](使用JAXB转换XML)
前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/spring ...
- SpringMVC Jackson 库解析 json 串属性名大小写自动转换问题
问题描述 在项目开发中,当实体类和表中定义的某个字段为 RMBPrice,首字母是大写的,sql 查询出来的列名也是大写的 RMBPrice,但是使用 jquery 的 ajax 返回请求响应时却出错 ...
- struts2(三)之表单参数自动封装与参数类型自动转换
前言 对struts2的使用不外乎这几点,参数自动封装,拦截器的使用,数据校验,ognl表达(值栈和actionContext的讲解),struts2的标签,struts2的国际化, struts2的 ...
随机推荐
- silverlight 打印
加引用: using System.Windows.Printing; xaml文件里: //定义图片和文本打印变量 PrintDocument printImage; public BeginCo ...
- hdu 2438
Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...
- mysql索引之组合索引
多列索引又称组合索引,在mysql的查询操作中,我们经常会遇到多个搜索条件,如:$sql = "select * from article where content='ma4' and t ...
- Mahout快速入门教程
Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...
- 阅读书目_2014H1
1.<程序员修炼之道 专业程序员必知的33个技巧>(完成) 注:更多是面向程序员全工作流程的. 2.linux shell脚本攻略 适合初学,但不方便作为参考手册查阅. 3.编写可读代码的 ...
- 使用Hexo搭建GitPage
资料: hexo官方文档:https://hexo.io/zh-cn/docs/ jekyll官方文档:http://jekyll.com.cn/docs/home/ 简介: 使用hexo和jekyl ...
- 关于viewWithTag的一点说明
通常我们使用viewWithTag如下情形: 如果我们用了一个父View,上面放了多个子view, 每个子view都通过从0开始的Tag值来进行标志,以便于后期在像View上直接使用viewWithT ...
- 导入Excel表格到SharePoint站点
在Excel中创建表 选中所有要导入的行,点击菜单"插入"->"表格" 如果数据内容包含表头,选择"我的表格包含表头". 选择菜单 ...
- Android模仿微信语音聊天功能
项目效果如下: 项目目录结构如下: 代码如下: AudioManager.java import java.io.File; import java.io.IOException; import ja ...
- 使用httpclient抓取时,netstat 发现很多time_wait连接
http://wiki.apache.org/HttpComponents/FrequentlyAskedConnectionManagementQuestions 1. Connections in ...