二、Swift网络请求回来的数据我这样取

网络请求框架Alamofire

源码地址
Swift 2.3 Alamofire3.0版本支持 iOS 8
Swift 3 Alamofire4.0以上版本支持 iOS 9及以上系统

json数据:

 {
    data =     {
        cate =         (
                        {
                grade = 1;
                id = 1000;
                name = "\U89e3\U578b\U5e73\U7ec6";
                parId = "";
                picUrl = "http://fpoimg.com/300x600?&bg_color=9ef279&text_color=f279c1&text=Gary";
                subList =                 (
                                        {
                        grade = 2;
                        id = 10000;
                        name = "\U652f\U754c\U7a0b\U5176\U610f\U53c8";
                        parId = 1;
                        picUrl = "http://fpoimg.com/336x280?&bg_color=79e5f2&text_color=f2db79&text=Jennifer";
                    },
                                        {
                        grade = 2;
                        id = 10001;
                        name = "\U519c\U8d77\U8bbe\U8c61";
                        parId = 2;
                        picUrl = "http://fpoimg.com/250x250?&bg_color=b879f2&text_color=79f295&text=Frank";
                    },
                                        {
                        grade = 2;
                        id = 10002;
                        name = "\U5b66\U6548\U90fd\U6700\U51b3\U957f";
                        parId = 3;
                        picUrl = "http://fpoimg.com/120x90?&bg_color=f27980&text_color=79a3f2&text=Nancy";
                    },
                                        {
                        grade = 2;
                        id = 10003;
                        name = "\U65cf\U4e00\U822c\U91c7\U786e";
                        parId = 4;
                        picUrl = "http://fpoimg.com/125x125?&bg_color=c7f279&text_color=f279ea&text=Cynthia";
                    },
                                        {
                        grade = 2;
                        id = 10004;
                        name = "\U6df1\U5f97\U519c\U8fd9\U90e8\U7406";
                        parId = 5;
                        picUrl = "http://fpoimg.com/120x600?&bg_color=79f2d6&text_color=f2b379&text=Anthony";
                    }
                );
            },
                        {
                grade = 1;
                id = 1001;
                name = "\U8d70\U5f20\U5171\U59d4";
                parId = "";
                picUrl = "http://fpoimg.com/720x300?&bg_color=8f79f2&text_color=85f279&text=Robert";
                subList =                 (
                                        {
                        grade = 2;
                        id = 10005;
                        name = "\U9053\U597d\U5bb6\U901a\U6bdb\U4e5d";
                        parId = 6;
                        picUrl = "http://fpoimg.com/240x400?&bg_color=f279a8&text_color=79ccf2&text=Mark";
                    },
                                        {
                        grade = 2;
                        id = 10006;
                        name = "\U738b\U8bc6\U4f8b\U91cc\U8054\U5df2";
                        parId = 7;
                        picUrl = "http://fpoimg.com/88x31?&bg_color=eff279&text_color=d179f2&text=George";
                    },
                                        {
                        grade = 2;
                        id = 10007;
                        name = "\U91cf\U738b\U5468\U7ba1\U672f";
                        parId = 8;
                        picUrl = "http://fpoimg.com/120x240?&bg_color=79f2ae&text_color=f28a79&text=Matthew";
                    },
                                        {
                        grade = 2;
                        id = 10008;
                        name = "\U4e1c\U529b\U957f\U56fd\U8d70\U88ab";
                        parId = 9;
                        picUrl = "http://fpoimg.com/234x60?&bg_color=798af2&text_color=aef279&text=Mary";
                    },
                                        {
                        grade = 2;
                        id = 10009;
                        name = "\U6280\U97f3\U4e8b\U6e29\U65f6";
                        parId = 10;
                        picUrl = "http://fpoimg.com/120x240?&bg_color=f279d1&text_color=79f2ef&text=Brian";
                    }
                );
            },
                        {
                grade = 1;
                id = 1002;
                name = "\U65b9\U5b66\U8fd1\U7ed9";
                parId = "";
                picUrl = "http://fpoimg.com/728x90?&bg_color=f2cc79&text_color=a979f2&text=Richard";
                subList =                 (
                                        {
                        grade = 2;
                        id = 10010;
                        name = "\U7b97\U6e29\U987b\U9762";
                        parId = 11;
                        picUrl = "http://fpoimg.com/180x150?&bg_color=79f285&text_color=f2798f&text=John";
                    },
                                        {
                        grade = 2;
                        id = 10011;
                        name = "\U8bb8\U62c9\U8d44\U4e13\U6708\U5f53\U533a";
                        parId = 12;
                        picUrl = "http://fpoimg.com/180x150?&bg_color=79b3f2&text_color=d6f279&text=Lisa";
                    },
                                        {
                        grade = 2;
                        id = 10012;
                        name = "\U6cbb\U5728\U4fe1\U773c\U65ad";
                        parId = 13;
                        picUrl = "http://fpoimg.com/728x90?&bg_color=ea79f2&text_color=79f2c7&text=Jeffrey";
                    },
                                        {
                        grade = 2;
                        id = 10013;
                        name = "\U5efa\U653f\U7ed3\U652f\U7acb\U5f80\U52a8";
                        parId = 14;
                        picUrl = "http://fpoimg.com/250x250?&bg_color=f2a379&text_color=8079f2&text=Edward";
                    },
                                        {
                        grade = 2;
                        id = 10014;
                        name = "\U53d1\U5c42\U5907\U4fe1\U6cbb\U91cd";
                        parId = 15;
                        picUrl = "http://fpoimg.com/120x90?&bg_color=95f279&text_color=f279b8&text=Dorothy";
                    }
                );
            }
        );
        pic =         (
                        {
                link = "http://iuvpwyufd.sc/auyydlnn";
                picUrl = "http://fpoimg.com/250x250?&bg_color=79dbf2&text_color=f2e579&text=Thomas";
                price = "759.4";
                prodId = 52037;
                storeId = 72788;
                text1 = "\U7ec7\U73b0\U79d1\U6ca1\U9a8c\U89c1\U79f0";
                text2 = "\U5411\U8d28\U53c2\U770b\U5236\U7c7b\U7c73";
                type = 2;
            },
                        {
                link = "http://gnba.ng/jwiv";
                picUrl = "http://fpoimg.com/468x60?&bg_color=c279f2&text_color=79f29e&text=Anthony";
                price = "219.1";
                prodId = 52038;
                storeId = 72789;
                text1 = "\U9a8c\U51e0\U653e\U8d8a\U518d";
                text2 = "\U8272\U9769\U53eb\U8981\U7a7a";
                type = 1;
            },
                        {
                link = "http://gjf.eh/xdx";
                picUrl = "http://fpoimg.com/240x400?&bg_color=f27b79&text_color=799af2&text=Ronald";
                price = "182.3";
                prodId = 52039;
                storeId = 72790;
                text1 = "\U8ba1\U503c\U5185\U65e0\U4e5f\U79d1\U67e5";
                text2 = "\U4e2d\U5468\U5fc3\U4e3b\U51e0\U79cd";
                type = 1;
            }
        );
    };
    msg = "\U6210\U529f";
    ret = 1;
}

一、系统自带的KVC

模型:

import UIKit

class BaseModel: NSObject {
    
    init(dict: [String: AnyObject]) {
        super.init()
        //setValuesForKeys是KVC方法
        //KVC的方法又是OC的方法,在运行时给对象发送消息,这点要求对象已经实例化完成。super.init()就是用来保证对象初始化完成。
        setValuesForKeys(dict)
        
    }
    //重写父类的方法
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        //没有调用super,将父类的代码完全覆盖,不会崩溃
    }
}

class TypesSATMD: BaseModel {
    var msg: String = ""
    var ret: Int = 0
    var data: [String: NSObject]? {
        didSet {
            guard let tempData = data else { return }
            typesSATData = TypesSATData(dict: tempData)
        }
    }
    
    var typesSATData: TypesSATData?
}

class TypesSATData: BaseModel {
    var cate: [[String: NSObject]]? {
        didSet {
            guard let tempCate_list = cate else { return }
            for dict in tempCate_list {
                cates.append(TypesSATCate(dict: dict))
            }
        }
    }
    
    var pic: [[String: NSObject]]? {
        didSet {
            guard let banner_list = pic else { return }
            for dict in banner_list {
               pics.append(TypesSATPic(dict: dict))
            }
        }
    }
    var cates: [TypesSATCate] = [TypesSATCate]()
    var pics:[TypesSATPic] =  [TypesSATPic]() // 轮播图数组
    
}

class TypesSATCate: BaseModel {
    var grade: Int = 0
    var id: NSNumber = NSNumber()
    var name: String = ""
    var parId: String = ""
    var picUrl: String = ""
    var subList: [[String: NSObject]]? {
        didSet {
            guard let tempSubList = subList else { return }
            for dict in tempSubList {
                subLists.append(TypesSATSubList(dict: dict))
            }
        }
    }
    
    var subLists: [TypesSATSubList] = [TypesSATSubList]()
}

class TypesSATPic: BaseModel {
    var link: String = ""
    var picUrl: String = ""
    var price: Double = 0
    var prodId: NSNumber = NSNumber()
    var storeId: NSNumber = NSNumber()
    var text1: String = ""
    var text2: String = ""
    var type: Int = 0
    
}

class TypesSATSubList: BaseModel {
    var grade: Int = 0
    var id: NSNumber = NSNumber()
    var name: String = ""
    var parId: NSNumber = NSNumber()
    var picUrl: String = ""
}

使用 eg:

 let type: TypesSATMD = TypesSATMD(dict: (result as? [String : NSObject])!)
 print(type.typesSATData?.pics.first?.picUrl ?? "")

二、SwiftyJSON

源码:https://github.com/SwiftyJSON/SwiftyJSON.git
初始化:

let json = JSON(data: dataFromNetworking)

let json = JSON(jsonobject)

if let dataFromString = jsonString.data(using: .utf8, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
}

一些常用的方法:

//从 JSON数组 中获取一个 double
let name = json[0].double

//用来显示 JSON 中是否有指定的值
if json["name"].exist()


三、ObjectMapper

源码:https://github.com/Hearst-DD/ObjectMapper.git
导入头文件:import ObjectMapper
模型继承:Mappable
实现两个协议方法:

    required init?(map: Map){ 
    }
    
    func mapping(map: Map) {
    }

模型:

import UIKit
import ObjectMapper

class TypeMD: Mappable {

    var ret: Int = 0

    var data: TypeData?

    var msg: String = ""
    
    required init?(map: Map){
        
    }
    
    func mapping(map: Map) {
        ret <- map["ret"]
        data <- map["data"]
        msg <- map["msg"]
    }
    
}

class TypeData: Mappable {

    var pic: [TypePic] = [TypePic]()

    var cate: [TypeCate] = [TypeCate]()
    
    required init?(map: Map){
        
    }
    
    func mapping(map: Map) {
        pic <- map["pic"]
        cate <- map["cate"]
    }

}

class TypePic: Mappable {

    var picUrl: String = ""

    var prodId: Int = 0

    var text1: String = ""

    var price: Int = 0

    var text2: String = ""

    var storeId: Int = 0

    var link: String = ""

    var type: Int = 0
    
    required init?(map: Map){
        
    }
    
    func mapping(map: Map) {
        picUrl <- map["picUrl"]
    }

}

class TypeCate: Mappable {

    var picUrl: String = ""

    var id: Int = 0

    var subList: [TypeSublist] = [TypeSublist]()

    var grade: Int = 0

    var name: String = ""

    var parId: String = ""
    
    required init?(map: Map){
        
    }
    
    func mapping(map: Map) {
        name <- map["name"]
    }

}

class TypeSublist: Mappable {

    var parId: Int = 0

    var id: Int = 0

    var grade: Int = 0

    var name: String = ""

    var picUrl: String = ""
    
    required init?(map: Map){
        
    }
    
    func mapping(map: Map) {
        name <- map["name"]
    }

}

使用eg:

 let test1 = Mapper<TypeMD>().map(JSONObject: result)
 print(test1?.data?.cate.first?.name ?? "")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容