EVReflection: easier way to parse JSON

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

下面咱們經過一個獲取糯米網城市列表的Demo來了解EVReflection的用法ruby

添加依賴

這裏咱們經過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

咱們先打印一下結果,分析一下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類

接下來咱們來寫咱們的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
}
相關文章
相關標籤/搜索