解決json問題。我一直在尋找一段時間如何解決這個問題,但沒有成功。Swift 3 Alamofire Swifty json解析問題
我的JSON輸出是這樣的:
{
"data": [{
"type": "schedules",
"id": "1",
"attributes": {
"date": "2016-12-24"
},
"relationships": {
"user": {
"data": {
"type": "users",
"id": "8"
}
},
"statuses": {
"data": [{
"type": "statuses",
"id": "2"
}]
},
"events": {
"data": [{
"type": "events",
"id": "7"
}]
}
}
}, {
"type": "schedules",
"id": "2",
"attributes": {
"date": "2016-12-04"
},
"relationships": {
"user": {
"data": {
"type": "users",
"id": "8"
}
},
"statuses": {
"data": [{
"type": "statuses",
"id": "12"
}, {
"type": "statuses",
"id": "16"
}, {
"type": "statuses",
"id": "17"
}]
},
"events": {
"data": [{
"type": "events",
"id": "16"
}, {
"type": "events",
"id": "17"
}, {
"type": "events",
"id": "1"
}]
}
}
}, {
"type": "schedules",
"id": "3",
"attributes": {
"date": "2002-12-03"
},
"relationships": {
"user": {
"data": {
"type": "users",
"id": "7"
}
},
"statuses": {
"data": [{
"type": "statuses",
"id": "3"
}, {
"type": "statuses",
"id": "11"
}]
},
"events": {
"data": [{
"type": "events",
"id": "4"
}, {
"type": "events",
"id": "19"
}]
}
}
}],
"included": [
{
"type": "events",
"id": "6",
"attributes": {
"name": "streamline leading-edge portals",
"event_type": "3",
"start_time": "22:20:04",
"end_time": "20:19:22"
}
}, {
"type": "events",
"id": "11",
"attributes": {
"name": "maximize dynamic niches",
"event_type": "8",
"start_time": "15:51:06",
"end_time": "22:24:56"
}
}, {
"type": "events",
"id": "12",
"attributes": {
"name": "transition vertical methodologies",
"event_type": "1",
"start_time": "19:55:59",
"end_time": "00:27:42"
}
}
]
}
我迅速獲得這看起來是這樣的:
func getSchedules<T: JSONObject>(_ success: @escaping ([T]) -> Void) {
self.getAllScheduleData { (json) in
var items: [T] = []
if let jsonArray = json.array {
for jsonItem in jsonArray {
items.append(T.fromJSONComplete(json: jsonItem) as! T)
}
}
success(items)
}
}
self.getAllScheduleData不使用Alamofire
這裏的問題API調用是,json.array永遠是空的。據我瞭解,它應該總是像這樣:json [「items」]。array。
但是這個json沒有頂層的調用。即物品。
我現在的選擇是獲取json [「data」]或json [「included」]。但我想讓他們都解析它到一個對象。
不知道如何解釋這更好,不幸的是 如果你需要更多的信息,請問。
我使用:斯威夫特3,Alamofire,SwiftyJSON
編輯: 我也曾考慮從電話獲取JSON後到頂層添加到它。 即items {data {},included {}} 但無法做到這一點正確
如果我使用for在Emptyless提示的語法,我可能必須更改此方法的簽名,對不對?
static func fromJSONComplete<T : Object>(json: JSON) -> T {
let s = Schedule()
if let id = json["data"]["id"].string {
s.id = id
}
if let date = json["data"]["attributes"]["date"].string {
s.date = date
}
if let type = json["data"]["type"].string {
s.type = type
}
if let includedArray = json["included"].array {
for userJson in includedArray {
if userJson["type"].string == "users" {
let user : User = User.fromJSON(json: userJson)
s.users.append(user)
}
}
for statusJson in includedArray {
if statusJson["type"].string == "statuses" {
let status : Status = Status.fromJSON(json: statusJson)
s.statuses.append(status)
}
}
for eventJson in includedArray {
if eventJson["type"].string == "events" {
let event : Event = Event.fromJSON(json: eventJson)
s.events.append(event)
}
}
}
return s as! T
}
預先感謝您!
克里斯
您是否嘗試過for for語法? for(key,subJSON)在json {}中。然後你可以使用json [key]迭代鍵「data」和「included」。 – Emptyless
@Emptyless我試過了,但我想我不完全理解它是如何使用的。你能給我一個代碼示例嗎? – Chrizlee