json을 구문 분석하는 데 문제가 있습니다. 나는이 문제를 해결하는 방법을 찾고 있었지만 성공하지 못했습니다.Swift 3 Alamofire Swingty 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"] 배열.그러나이 json에는 전화 할 최상위 계층이 없습니다. 즉 항목.
내 옵션은 이제 json [ "data"] 또는 json [ "included"] 중 하나를 얻습니다. 그러나 나는 그것들을 객체와 함께 파싱하도록하고 싶다.
불행히도 이것을 잘 설명하는 방법이 확실하지 않습니다. 자세한 정보가 필요하면 문의하십시오.
내가 사용하고 : 스위프트 3, Alamofire, SwiftyJSON
편집 : 나는 또한 호출에서 JSON을 받고 이후에 상위 레이어를 추가로 간주 한 . 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 (key, subJSON) for json {}. 그런 다음 json [key]를 사용하여 "data"및 "included"키를 반복 할 수 있습니다. – Emptyless
@Emptyless 시도해 보았지만 사용 방법을 완전히 이해하지 못했다고 생각합니다. 코드 예제를 알려주시겠습니까? – Chrizlee