JSON解析是App开发时常常会遇到的需求,绝大部分网络请求的返回数据都是以JSON的形式,手动写JSON解析的代码费时费力,写出不少丑陋的代码。EVReflection提供了一种更加优雅简单的JSON解析方式json
EVReflection能够自动的将符合EVObject的类从JSON反序列化
首先须要让本身的Model类继承EVObjectswift
class User: EVObject { var id: Int = 0 var name: String = "" var friends: [User]? = [] }
单个对象的反序列化api
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来了解EVReflection的用法ruby
这里咱们经过CocoaPods来管理依赖网络
pod 'EVReflection' pod 'Alamofire'
咱们先新建一个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) } } }
咱们先打印一下结果,分析一下JSONcode
{ 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须要注意的是,咱们定义的数据类必定要继承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 }