SwiftJson的使用
项目中为什么需要导入SwiftyJSON呢?我一个个解析不可以吗?当然可以,以下会有一个对比
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { getRequestData(url: "https://taoke.applinzi.com/taokelink.php?pid=mm_133573663_70000234_14094850291&itemid=8345635009", parame: [:]) } /** 这里是返回值的格式: { "ret": "SUCCESS", "msg": null, "clickUrl": "//s.click.taobao.com/t?e=m%3D2%26s%3DtrxmUiN46thw4vFB6t2Z2ueEDrYVVa64K7Vc7tFgwiHjf2vlNIV67pOBzQ4CGOHIUOZr0mLjuseGoasII9DMoPzZVtgLka0946q6FVfTMSCcLWG8AJtyin9RXAoUH1mA7v2fHg6o8i5FXZpdkpUelRstnv%2BTtgu5IYULNg46oBA%3D&union_lens=lensId:0b1dabb2_0e69_16ea6dd3958_6643", "shareUrl": "//uland.taobao.com/coupon/edetail?e=lDNI9o6sBHcR%2BxmP1xMF4b%2BYIXJmUhfDtvj0rTZLUUqnU%2FfLOrzsNxIuTyg8ob7bXvNVQjWcvSceg%2F%2FpBZRTqw0F6oz2SutXgBwRBZjG0mkz84bgvNVmYg%3D%3D" } */ func getRequestData(url:String,parame:[String:String]){ Alamofire.request(url, method: .get, parameters: parame, encoding: URLEncoding.queryString, headers: nil).responseJSON { (response) in if response.result.isSuccess{ print("成功了") if let value1 = response.result.value as? [String: AnyObject]{ // print("clickUrl : \(value1["clickUrl"]!)") if let msg = value1["msg"] as? String{ // print("msg是有值的:\(msg)") }else{ // print("msg是无值的") } } } } }
首先我们需要强转,在使用if进行可选绑定,很繁琐,这里只有一层,如果是多层,则更加繁琐,如果是用Json的话,就非常简单了,我们不需要考虑取值的饿时候不需要考虑该值是不是存在,做个对比吧,首先导入SwiftJson
pod 'SwiftyJSON'
解析:
if response.result.isSuccess{ print("成功了") let json:JSON = JSON(response.value!); print("msg:\(json["msg"].rawString()) clickUrl:\(json["clickUrl"].rawString())") /** msg:null clickUrl://s.click.taobao.com/t?e=m%3D2%26s%3DtrxmUiN46thw4vFB6t2Z2ueEDrYVVa64K7Vc7tFgwiHjf2vlNIV67pOBzQ4CGOHIUOZr0mLjuseGoasII9DMoPzZVtgLka0946q6FVfTMSCcLWG8AJtyin9RXAoUH1mA7v2fHg6o8i5FXZpdkpUelRstnv%2BTtgu5IYULNg46oBA%3D&union_lens=lensId:0b1dabb2_0e69_16ea6dd3958_6643 */ }
这里的输出值是
成功了 clickUrl : //s.click.taobao.com/t?e=m%3D2%26s%3DtrxmUiN46thw4vFB6t2Z2ueEDrYVVa64K7Vc7tFgwiHjf2vlNIV67pOBzQ4CGOHIUOZr0mLjuseGoasII9DMoPzZVtgLka0946q6FVfTMSCcLWG8AJtyin9RXAoUH1mA7v2fHg6o8i5FXZpdkpUelRstnv%2BTtgu5IYULNg46oBA%3D&union_lens=lensId:0b1dabb2_0e69_16ea6dd3958_6643 msg是无值的
所以项目中为了简单方便我们会使用SwiftJson来进行三方解析,以上很简单,以下会对SwiftJson进行复杂一点的解析,多层解析,以下是Demo
这里是逐层解析
let json = JSON([ ["name":"Jack", "age": 25,"phones":[["name":"公司","number":"222222"],["name":"家庭","number":"777777"]]], ["name":"John", "age": 20,"phones":[["name":"公司","number":"111111"],["name":"家庭","number":"666666"]]] ]) for (index,subJson):(String, JSON) in json { print("index:\(index) name:\(subJson["name"]) age:\(subJson["age"])") for (subIndex,phonesJson):(String, JSON) in subJson["phones"] { print("subIndex:\(subIndex) name:\(phonesJson["name"]) number:\(phonesJson["number"]) )") } }
输出
index:0 name:Jack age:25 subIndex:0 name:公司 number:222222 ) subIndex:1 name:家庭 number:777777 ) index:1 name:John age:20 subIndex:0 name:公司 number:111111 ) subIndex:1 name:家庭 number:666666 )
也可以单个解析:
var json = JSON([ ["name":"Jack", "age": 25,"phones":[["name":"公司","number":"222222"],["name":"家庭","number":"777777"]]], ["name":"John", "age": 20,"phones":[["name":"公司","number":"111111"],["name":"家庭","number":"666666"]]] ]) print(json[0]["phones"][0]["number"])//输出:222222 print(json[0]["phones"][0]["number1"])//输出:null json[0]["phones"][0]["number1"] = "888888"//说明可以赋值,前提是json声明使用var print(json[0]["phones"][0]["number1"])//输出:888888 // 判断一个值是否存在,因为我数组里面就两个值,我使用2就会发生数据越界,但是使用SwiftJson就不会有问题 if(json[0]["phones"][2]["number"].stringValue.isEmpty){ print("这里代表不存在") }else{ print(json[0]["phones"][2]["number"].stringValue) } //输出:这里代表不存在