JSON解析是App开发时经常会遇到的需求,绝大部分网络请求的返回数据都是以JSON的形式,手动写JSON解析的代码费时费力,写出很多丑陋的代码。EVReflection提供了一种更加优雅简单的JSON解析方式
使用简介
EVReflection可以自动的将符合EVObject的类从JSON反序列化
首先需要让自己的Model类继承EVObject
class User: EVObject {
var id: Int = 0
var name: String = ""
var friends: [User]? = []
}
单个对象的反序列化
let json:String = "{\"id\": 24, \"name\": \"Bob Jefferson\", \"friends\": [{\"id\": 29, \"name\": \"Jen Jackson\"}]}"
let user = User(json: json)
数组的反序列化
let json:String = "[{\"id\": 27, \"name\": \"Bob Jefferson\"}, {\"id\": 29, \"name\": \"Jen Jackson\"}]"
let array = [User](json: json)
Demo
下面我们通过一个获取糯米网城市列表的Demo来了解EVReflection的用法
添加依赖
这里我们通过CocoaPods来管理依赖
pod 'EVReflection'
pod 'Alamofire'
ServiceProxy类
我们先新建一个ServiceProxy类,将我们的网络请求部分代码放进去
import Foundation
import EVReflection
import Alamofire
class ServiceProxy{
// MARK: -URL
static var ServiceEndpointBase : String {
return "http://apis.baidu.com/baidunuomi/openapi/cities"
}
class func getCityList (complete:(response: AnyObject?, error: NSError?) -> Void){
Alamofire.request(.GET, ServiceEndpointBase, parameters: parameters, encoding: .URL, headers: ["apikey":"ownAPIKey"]).validate(statusCode: 200..<300).responseJSON { (response:Response<AnyObject, NSError>) -> Void in
print(response.result.value!)
complete(response: response.result.value, error: response.result.error)
}
}
}
分析JSON
我们先打印一下结果,分析一下JSON
{
cities = (
{
"city_id" = 100010000;
"city_name" = "\U5317\U4eac\U5e02";
"city_pinyin" = beijing;
"short_name" = "\U5317\U4eac";
"short_pinyin" = bj;
},
{
"city_id" = 500010000;
"city_name" = "\U5929\U6d25\U5e02";
"city_pinyin" = tianjin;
"short_name" = "\U5929\U6d25";
"short_pinyin" = tj;
},
{
"city_id" = 1800010000;
"city_name" = "\U77f3\U5bb6\U5e84\U5e02";
"city_pinyin" = shijiazhuang;
"short_name" = "\U77f3\U5bb6\U5e84";
"short_pinyin" = sjz;
},
{
"city_id" = 1800020000;
"city_name" = "\U5510\U5c71\U5e02";
"city_pinyin" = tangshan;
"short_name" = "\U5510\U5c71";
"short_pinyin" = ts;
},
{
"city_id" = 1800030000;
"city_name" = "\U79e6\U7687\U5c9b\U5e02";
"city_pinyin" = qinhuangdao;
"short_name" = "\U79e6\U7687\U5c9b";
"short_pinyin" = qhd;
},
{
"city_id" = 1800040000;
"city_name" = "\U90af\U90f8\U5e02";
"city_pinyin" = handan;
"short_name" = "\U90af\U90f8";
"short_pinyin" = hd;
},
{
"city_id" = 1800050000;
"city_name" = "\U90a2\U53f0\U5e02";
"city_pinyin" = xingtai;
"short_name" = "\U90a2\U53f0";
"short_pinyin" = xt;
},
{
"city_id" = 1800060000;
"city_name" = "\U4fdd\U5b9a\U5e02";
"city_pinyin" = baoding;
"short_name" = "\U4fdd\U5b9a";
"short_pinyin" = bd;
},
{
"city_id" = 1800070000;
"city_name" = "\U5f20\U5bb6\U53e3\U5e02";
"city_pinyin" = zhangjiakou;
"short_name" = "\U5f20\U5bb6\U53e3";
"short_pinyin" = zjk;
},
{
"city_id" = 1800080000;
"city_name" = "\U627f\U5fb7\U5e02";
"city_pinyin" = chengde;
"short_name" = "\U627f\U5fb7";
"short_pinyin" = chengde;
},
{
"city_id" = 1800090000;
"city_name" = "\U6ca7\U5dde\U5e02";
"city_pinyin" = cangzhou;
"short_name" = "\U6ca7\U5dde";
"short_pinyin" = cangzhou;
},
{
"city_id" = 1800100000;
"city_name" = "\U5eca\U574a\U5e02";
"city_pinyin" = langfang;
"short_name" = "\U5eca\U574a";
"short_pinyin" = lf;
}
......
);
errno = 0;
msg = success;
}
创建Model类
接下来我们来写我们的Model需要注意的是,我们定义的数据类一定要继承EVObject
import Foundation
import EVReflection
class CityModel:EVObject{
var city_name = ""
var city_pinyin = ""
var short_name = ""
var short_pinyin = ""
var city_id = 100010000
}
测试结果
最后我们在viewDidLoad方法中测试一下城市列表的获取情况
JSON类型由SwiftyJSON提供
ServiceProxy.getCityList({ (response, error) -> Void in
let array = [CityModel](json: JSON(response!)["cities"].rawString())
print(array)
})
最后得到的数据的结果如下
CityModel {
hash = -1349730932489599961
key = short_name, value = 乐平
key = city_pinyin, value = leping
key = city_name, value = 乐平市
key = short_pinyin, value = leping
key = city_id, value = 2400130000
}