Swift 3 Alamofire Swifty json解析问题

时间:2017-01-22 17:54:00

标签: json parsing swift3 alamofire

解析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 [&#34; items&#34;]。array。

然而这个json没有可以调用的顶层。即物品。

我现在的选择是获得json [&#34;数据&#34;]或者json [&#34;包含&#34;]。但我想让它们都解析为一个对象。

不幸的是,不确定如何更好地解释这一点 如果您需要更多信息,请询问。

我正在使用:Swift 3,Alamofire,SwiftyJSON

修改 我还考虑过从调用中获取json后添加一个顶层。 项目{data {},包括{}} 但没有成功做到

如果我按照Emptyless的建议使用for语法,我可能需要更改此方法的签名,对吧?

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
    }

提前谢谢!

克里斯

1 个答案:

答案 0 :(得分:0)

请求的代码示例:

for (key,subjson) in json {
    for i in 1 ..< json[key].count where subjson.array != nil {
        print("found json for key: \(key): \(json[key][i])")
    }
}

它检查提供的json对象并迭代可用的顶键。如果键包含某个对象的数组,它也会迭代它。

我主要使用的方法是打开密钥并将json对象作为初始化程序传递,但它也可以用于分割函数。

e.g。

switch(key) {
case "included":
     // Do something
case "data":
    // Do Something
default:
    break
}