我需要解析来自 Foursquare API 的 JSON 响应,我正在使用 swift:
我有发出 API 请求的代码,我得到以下响应:
Optional({"meta":{"code":200,"requestId":"56d2a675498e93c71cfeadc5"},"response":{"venues":[{"id":"4f629435e4b086a33576e89e","name":"El Rinconcito Poblano","contact":{},"location":{"lat":16.735342,"lng":-92.038453,"distance":21267,"cc":"MX","country":"Mexico","formattedAddress":["Mexico"]},"categories":[{"id":"4bf58dd8d48988d1c1941735","name":"Mexican Restaurant","pluralName":"Mexican Restaurants","shortName":"Mexican","icon":{"prefix":"https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/mexican_","suffix":".png"},"primary":true}],"verified":false,"stats":{"checkinsCount":10,"usersCount":8,"tipCount":0},"allowMenuUrlEdit":true,"specials":{"count":0,"items":[]},"hereNow":{"count":0,"summary":"Nobody here","groups":[]},"referralId":"v-1456645749","venueChains":[]},{"id":"502aae9de4b059b606a00f7d","name":"Mariscos","contact":{},"location":{"lat":16.73153051714288,"lng":-92.04085495810608,"distance":21313,"cc":"MX","country":"Mexico","formattedAddress":["Mexico"]},"categories":[{"id":"4bf58dd8d48988d1ce941735","name":"Seafood Restaurant","pluralName":"Seafood Restaurants","shortName":"Seafood","icon":{"prefix":"https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/seafood_","suffix":".png"},"primary":true}],"verified":false,"stats":{"checkinsCount":2,"usersCount":2,"tipCount":0},"allowMenuUrlEdit":true,"specials":{"count":0,"items":[]},"hereNow":{"count":0,"summary":"Nobody here","groups":[]},"referralId":"v-1456645749","venueChains":[]},{"id":"51c47ba9498efee47ed447ce","name":"Tricos","contact":{},"location":{"address":"Justo Sierra","lat":16.543866,"lng":-92.055473,"distance":23967,"cc":"MX","country":"Mexico","formattedAddress":["Justo Sierra","Mexico"]},"categories":[{"id":"4bf58dd8d48988d112941735","name":"Juice Bar","pluralName":"Juice Bars","shortName":"Juice Bar","icon":{"prefix":"https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/juicebar_","suffix":".png"},"primary":true}],"verified":false,"stats":{"checkinsCount":0,"usersCount":0,"tipCount":0},"allowMenuUrlEdit":true,"specials":{"count":0,"items":[]},"hereNow":{"count":0,"summary":"Nobody here","groups":[]},"referralId":"v-1456645749","venueChains":[]},.....
我在 field 中得到了一个数组,我怎样才能得到每个 field 的名称或图片的数组?
谢谢。
最佳答案
这不需要库,没有它也可以很好地完成。
这是用简单的 swift 实现的一种方法
protocol JSONDecodable {
typealias DecodableType
static func decode(data: [NSString: AnyObject]) throws -> DecodableType
static func decode(array: [[NSString: AnyObject]]) -> [DecodableType]
}
enum DecodingError: ErrorType {
case InvalidInstance(error: String)
}
struct Photo {
let url: NSURL
}
struct Venue {
let id: String
let name: String
let photos: [Photo]
}
extension Venue: JSONDecodable {
typealias DecodableType = Venue
static func decode(data: [NSString : AnyObject]) throws -> DecodableType {
guard let id = data["id"] as? String,
let name = data["name"] as? String else {
throw DecodingError.InvalidInstance(error: "Keys `id` and `name` were not present and Strings")
}
return Venue(id: id, name: name, photos: [])
}
static func decode(array: [[NSString : AnyObject]]) -> [DecodableType] {
return array.flatMap {
try? Venue.decode($0)
}
}
}
我将把对照片的解析作为练习留给您,但它通常与解析 Venue 的想法相同。
关于arrays - Swift 解析 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680111/