iOS中JSONModel的使用

本文详细介绍了如何将JSONModel添加到iOS项目中,并通过实例展示了基本使用方法,包括需求环境、源码文档及基本使用步骤。

iOS中JSONModel的使用
Adding JSONModel to your project (https://github.com/icanzilb/JSONModel)
添加JSONModel到你的项目中
Requirements
需要的环境
ARC only; iOS 5.0+ / OSX 10.7+
SystemConfiguration.framework(需要导入系统库)

Get it as: 1) source files
Download the JSONModel repository as a zip file or clone it

下载JSONModel.zip文件
Copy the JSONModel sub-folder into your Xcode project

将它拷贝到你的项目中
Link your app to SystemConfiguration.framework

导入SystemConfiguration.framework框架

or 2) via Cocoa pods
使用Cocoa pods引入
In your project’s Podfile add the JSONModel pod:
pod ‘JSONModel’

If you want to read more about CocoaPods, have a look at this short tutorial.

如果你想关于CocoaPods了解更多,请参考这个简单的教程
Source code documentation
源码文档
The source code includes class docs, which you can build yourself and import into Xcode:

源码包含了类的文档,你也可以自己编译并且导入到xcode
If you don’t already have appledoc installed, install it with homebrew by typing brew install appledoc.

如果你还没有安装appledoc,先安装appledoc
Install the documentation into Xcode by typing appledoc .
in the root directory of the repository.

在xcode上安装appledoc文档,在根目录下
Restart Xcode if it’s already running.

重启xcode
Basic usage
基本使用
Consider you have a JSON like this:

假如你的JSON数据像这样:
{“id”:”10”, “country”:”Germany”, “dialCode”: 49, “isInEurope”:true}
Create a new Objective-C class for your data model and make it inherit the JSONModel class.

创建一个Objective-C类,继承自JSONModel
Declare properties in your header file with the name of the JSON keys:

将JSON中的keys在.h文件中声明为属性

import “JSONModel.h”

@interface CountryModel : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* country;
@property (strong, nonatomic) NSString* dialCode;
@property (assign, nonatomic) BOOL isInEurope;

@end
There’s no need to do anything in the .m file.

在.m文件中不需要做任何事情
Initialize your model with data:

用数据初始化你的model

import “CountryModel.h”

NSString* json = (fetch here JSON from Internet) …
NSError* err = nil;
CountryModel* country = [[CountryModel alloc] initWithString:json
error:&err];
If the validation of the JSON passes you have all the corresponding properties in your model populated from the JSON. JSONModel will
also try to convert as much data to the types you expect, in the example above it will:

如果传过来的JSON合法,你所定义的所有的属性都会与该JSON的值想对应,甚至JSONModel会尝试去转换数据为你期望的类型,如上所示:
convert “id” from string (in the JSON) to an int for your class
just copy country’s value

转换id,从字符串转换为int
convert dialCode from number (in the JSON) to an NSString value

转换diaCode,从number转换为字符串
finally convert isInEurope to a BOOL for your BOOL property

最后一个是将isInEurope转换为BOOL属性
And the good news is all you had to do is define the properties and their expected types.

所以,你所需要做的就是将你的属性定义为期望的类型
Online tutorials

在线教程
Official website: http://www.jsonmodel.com
Class docs online: http://jsonmodel.com/docs/
Step-by-step tutorials:

傻瓜教程:
How to fetch and parse JSON by using data models

Performance optimisation for working with JSON feeds via JSONModel

How to make a YouTube app using MGBox and JSONModel

Examples
例子
Automatic name based mapping

命名自动匹配
{
“id”: “123”,
“name”: “Product name”,
“price”: 12.95
}

@interface ProductModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;
@end

@implementation ProductModel
@end

01、数据简介 出口韧性是地级市在面对外部震荡和压力时,能够承受并迅速适应、应对变化的能力。这种能力体现在地级市经济结构的灵活性、创新能力和竞争力,以及地方政府的政策支持和产业调整能力等多个方面。 城市出口韧性对于城市的经济发展、就业稳定、国际贸易地位以及风险抵御能力等方面都具有重要影响。因此,城市应加强出口韧性的建设,提高应对外部冲击的能力,以推动其经济的可持续发展。 数据名称:地级市-城市出口韧性数据 数据年份:2011-2022年 02、相关数据 代码 年份 地区 城市 省份 城市出口韧性 距离港口的最近距离 最终进口额_百万人民币2 最终出口额_百万人民币2 人均道路面积2 年末金融机构各项贷款余额万元2 地区生产总值万元2 科学支出万元2 地方财政一般预算内支出万元2 城镇居民人均可支配收入元2 固定资产投资2 实际使用外商投资额百万美元2 城镇化率2 外贸依存度 出口贸易 年平均汇率 实际使用外商投资额百万人民币2 外资依存度 金融发展水平 财政投资力度 科学技术水平 出口偏离度 x_地区生产总值万元2 x_城镇化率2 x_人均道路面积2 x_外贸依存度 x_出口贸易 x_出口偏离度 x_金融发展水平 x_城镇居民人均可支配收入元2 x_财政投资力度 x_科学技术水平 x_距离港口的最近距离 x_外资依存度 地区生产总值万元2_sum y_地区生产总值万元2 城镇化率2_sum y_城镇化率2 人均道路面积2_sum y_人均道路面积2 外贸依存度_sum y_外贸依存度 出口贸易_sum y_出口贸易 出口偏离度_sum y_出口偏离度 金融发展水平_sum y_金融发展水平 城镇居民人均可支配收入元2_sum y_城镇居民人均可支配收入元2 财政投资力度_sum y_财政投资力度 科学技术水平_sum y_科学技术水平
内容概要:本文档详细介绍了一个基于Matlab实现的无人机空中通信仿真资源包,系统涵盖了无人机通信、三维路径规划、状态估计与多机协同等多个核心技术模块的仿真代码与案例研究。内容聚焦于无人机在复杂环境下的三维路径规划(如基于遗传算法GA、粒子群算法PSO、动态窗口法DWA等)、无人机姿态与轨迹的状态估计算法(如扩展卡尔曼滤波器EKF、UKF、不变扩展卡尔曼滤波IEKF、粒子滤波PF等),以及无人机通信链路建模与优化,并融合智能优化算法对系统性能进行提升。此外,资源包还拓展至微电网优化、MIMO检测、图像融合、信号处理等相关科研领域,构建了一个以无人机技术为核心、多学科交叉融合的综合性仿真研究体系。; 适合人群:具备一定Matlab编程能力与控制系统基础知识,从事无人机系统设计、无线通信、自动化控制、智能优化算法或相关领域研究的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①开展无人机通信系统建模与性能仿真分析;②实现复杂动态环境中无人机三维路径规划与实时避障;③研究基于多源传感器融合的无人机导航与状态估计方法;④结合智能优化算法提升无人机任务执行效率与系统鲁棒性; 阅读建议:建议读者依据资源包提供的模块化结构系统学习,优先掌握Matlab/Simulink基本仿真技能,重点研读路径规划与状态估计部分的算法实现与代码细节,并通过实际调试与二次开发加深对无人机系统集成与优化策略的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值