JSON和第三方庫(kù)
用到的第三方庫(kù):
- ObjectiveC: MJExtension
- swift: Argo
JSON示例:
{
"author": [
{
"id": 6,
"name": "Gob Bluth"
},
{
"id": 7,
"name": "7Gob Bluth"
}
],
"text": "I've made a huge mistake.",
"comments": [
{
"author": {
"id": 1,
"name": "Lucille"
},
"text": "Really? Did 'nothing' cancel?"
}
]
}
JSON轉(zhuǎn)模型
在OC中,利用MJExtension等第三方庫(kù),我們能夠很方便的將JSON轉(zhuǎn)換為模型。
在swift中,就沒(méi)有這么方便了。因?yàn)闀?huì)有可選值(optional)類(lèi)型的影響。往往需要判斷是否是可選值,各種右移if let判斷,無(wú)法忍受。這里有篇文章 說(shuō)的很詳細(xì),還有對(duì)比當(dāng)前流行的庫(kù)的優(yōu)缺點(diǎn)。
SWIFT JSON SHOOT-OUT 文章鏈接
在swift中怎么進(jìn)行JSON轉(zhuǎn)模型操作呢?
- 橋接ObjectiveC的MJExtension到swift中,進(jìn)行轉(zhuǎn)換操作
- 使用swift版本的庫(kù)Argo,進(jìn)行轉(zhuǎn)換操作
橋接MJExtension到swift
大致步驟如下:
- 創(chuàng)建繼承自NSObject的模型Model,橋接在swift項(xiàng)目里。
- 用ESJsonFormat工具創(chuàng)建類(lèi)屬性。
- 在swift中,進(jìn)行轉(zhuǎn)換。
//獲取JSON數(shù)據(jù),然后轉(zhuǎn)換
let filePath = NSBundle.mainBundle().pathForResource("data", ofType: "json")
let contentData = NSFileManager.defaultManager().contentsAtPath(filePath!)
let content = NSString(data: contentData!, encoding: NSUTF8StringEncoding) as? String
//JSON轉(zhuǎn)模型
let model = Model.mj_objectWithKeyValues(content)
print(model.ShopList[0].ShopInfo.develiyTime)
使用Argo庫(kù)
Argo庫(kù)用函數(shù)式方法來(lái)轉(zhuǎn)換。不過(guò)里面用到了許多操作符,咋看上去簡(jiǎn)直嚇?biāo)廊恕2贿^(guò)熟悉后就好了。
像這樣的:
struct Author {
let id: Int
let name: String
}
extension Author: Decodable {
static func decode(json: JSON) -> Decoded<Author.DecodedType> {
return curry(self.init)
<^> json <| "id"
<*> json <| "name"
}
}
使用步驟:
- 創(chuàng)建模型結(jié)構(gòu)體
- 擴(kuò)展結(jié)構(gòu)體并遵守協(xié)議:
Decodable,實(shí)現(xiàn)協(xié)議方法decode(json: JSON)。并映射對(duì)應(yīng)關(guān)系。
操作符說(shuō)明:
“<|” 映射字符值。含義是轉(zhuǎn)換<|右邊對(duì)應(yīng)的屬性名,屬性名是String的。如:
<^> json <| "id",映射JSON中的id字段為id屬性。
“<|?” 映射可選字符值。就是說(shuō)轉(zhuǎn)換過(guò)來(lái)的值有可能是null
"<^>" map,映射的意思。具體什么用的,不是很了解,不過(guò),一般擴(kuò)展方法中,它都是第一個(gè)。
“<*>” apply和上一個(gè)類(lèi)似。
“<||” 轉(zhuǎn)換數(shù)組。這個(gè)是轉(zhuǎn)換JSON中的數(shù)組值用的。如:JSON {"array":["1","2"]} ,轉(zhuǎn)換的話就是: <|| ["array"]
“<||?” 轉(zhuǎn)換的數(shù)組是可選值,有可能是null.
示例的JSON,用Argo轉(zhuǎn)換為模型的代碼,看起來(lái)有點(diǎn)多。
//SwiftModel.swfit中
import UIKit
import Argo
import Curry
struct Author {
let id: Int
let name: String
}
extension Author: Decodable {
static func decode(json: JSON) -> Decoded<Author.DecodedType> {
return curry(self.init)
<^> json <| "id"
<*> json <| "name"
}
}
struct Comments {
let author: Author
let text: String
}
extension Comments: Decodable {
static func decode(json: JSON) -> Decoded<Comments.DecodedType> {
return curry(self.init)
<^> json <| "author"
<*> json <| "text"
}
}
struct SwiftModel {
let author: [Author]
let text: String
let comments: [Comments]
}
extension SwiftModel: Decodable {
static func decode(json: JSON) -> Decoded<SwiftModel.DecodedType> {
return curry(self.init)
<^> json <|| ["author"]
<*> json <| "text"
<*> json <|| ["comments"]
}
}
//viewController中
let json = JSONFromFile("data")
let argoModel: SwiftModel = json.flatMap(decode)!
print(argoModel.author[0].name)
樣例工程在這里