2016-02-28 78 views
0

我需要解析来自Foursquare的API的JSON响应,我使用SWIFT:斯威夫特解析JSON响应

我的代码进行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":[]},..... 

我在场馆有一个阵列,我怎么才能得到一系列名称或每个场地的图片?

谢谢。

+0

我会看看https://github.com/SwiftyJSON/SwiftyJSON来帮助你实现。 –

回答

2

有没有必要为此图书馆它可以做得很好,没有它。

下面是用普通迅速

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) 
     } 
    } 
} 

我会离开贯彻照片的分析作为练习,你做这件事的一种方式,但它通常是相同的思路解析地点。

+0

谢谢你的回答雨果,我想我需要更多的帮助。我有我的viewController里面的locationManager函数,用下面的代码我从Foursquare API得到答案:let task = NSURLSession.sharedSession()。(data,response,error)in let urlContent = NSString (data:data !,编码:NSUTF8StringEncoding) print(urlContent)//////////如何使用代码解析变量“urlContent” –

+1

您不想将数据解析为一个字符串,而不是看看使用[NSJSONSerialization.JSONObjectWithData](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/JSONObjectWithData:options:error :)这将返回一个字典或数组取决于json对象的根 –