博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二、Swift网络请求回来的数据我这样取
阅读量:5789 次
发布时间:2019-06-18

本文共 12035 字,大约阅读时间需要 40 分钟。

网络请求框架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 UIKitclass 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数组 中获取一个 doublelet 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 UIKitimport ObjectMapperclass 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
().map(JSONObject: result) print(test1?.data?.cate.first?.name ?? "")复制代码

转载地址:http://ahmyx.baihongyu.com/

你可能感兴趣的文章
安装webpack
查看>>
Python 抓取网页乱码问题 以及EXCEL乱码
查看>>
https连接加载http资源
查看>>
个人阅读作业2
查看>>
UE4材质是什么样的机制
查看>>
InfluxDB Cli中查询结果中time格式显示设置
查看>>
【Android】16.5 Android内置的系统服务
查看>>
Codeforces 994 C - Two Squares
查看>>
ruby 批量下载王者荣耀皮肤
查看>>
jq设置样式属性
查看>>
JavaScript学习——判断数据类型总结(转)
查看>>
tp中url地址大小写问题
查看>>
linux 下 php 安装 libevent
查看>>
忆高中三位良师
查看>>
使用QTP录制自带Flight小实例
查看>>
函数库管理
查看>>
Adding custom instruction to RISCV ISA and running it on gem5 and spike(转)
查看>>
JProfiler学习笔记
查看>>
Loadrunner脚本编程(4)-数据类型操作和字符串操作
查看>>
Codeforces Round #529 (Div. 3) 题解
查看>>