使用Zomato API密鑰,您如何進行身份驗證,搜索餐館並解析數據?如何使用Zomato API對數據進行身份驗證和解析
0
A
回答
0
見代碼示例:
let zomatoKey = "<Your API Key>"
let centerLatitude = 19.06558, centerLongitude = 72.86215
let urlString = "https://developers.zomato.com/api/v2.1/search?&lat=\(centerLatitude)&lon=\(centerLongitude)";
let url = NSURL(string: urlString)
if url != nil {
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(zomatoKey, forHTTPHeaderField: "user_key")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in
if error == nil {
let httpResponse = response as! NSHTTPURLResponse!
if httpResponse.statusCode == 200 {
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
if let restaurants = json["restaurants"] as? [NSDictionary] {
for rest in restaurants {
var searchResult = [String:AnyObject?]()
let restaurant = rest["restaurant"] as! NSDictionary
print(restaurant["id"] as? NSString)
print(restaurant["average_cost_for_two"] as? NSNumber)
print(restaurant["cuisines"] as? String)
print(restaurant["url"] as? String)
print(restaurant["thumb"] as? String)
if let location = restaurant["location"] as? NSDictionary {
print(location["address"] as? String)
print(location["city"] as? String)
print((location["latitude"] as? NSString).doubleValue)
print((location["longitude"] as? NSString).doubleValue)
}
print(restaurant["menu_url"] as? String)
print(restaurant["name"] as? String)
print(restaurant["phone_numbers"] as? String)
if let user_rating = restaurant["user_rating"] as? NSDictionary {
print(user_rating["aggregate_rating"] as? NSString)
print(user_rating["rating_color"] as? String)
print((user_rating["votes"] as? NSNumber)).doubleValue)
}
}
}
}
} catch {
print(error)
}
}
}
})
task.resume()
}
-1
這是我實現假設你有一個一流的餐廳
func getRestaurants() {
let url = NSURL(string: zomatoURLString)
if url != nil {
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(zomatoKey, forHTTPHeaderField: "user_key")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {
(data,response,error) -> Void in
if let error = error {
print(error)
return
}
//Parse Json data
if let data = data {
self.restaurants = self.parseJsonData(data)
//reload table
NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in
self.tableView.reloadData()
})
}
})
task.resume()
}
}
func parseJsonData(data: NSData) -> [Restaurant] {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
//Parse JSON DATA
let jsonRestaurants = jsonResult?["restaurants"] as! [AnyObject]
for res in jsonRestaurants {
let restaurant = Restaurant()
let myRestaurant = res["restaurant"] as! NSDictionary
restaurant.restaurantName = myRestaurant["name"] as! String
restaurant.restaurantType = myRestaurant["cuisines"] as! String
restaurant.restaurantImage = myRestaurant["thumb"] as! String
let location = myRestaurant["location"] as! [String: AnyObject]
restaurant.restaurantLocation = location["address"] as! String
restaurants.append(restaurant)
}
} catch {
print(error)
}
return restaurants
}
2
如果運行您的遊樂場代碼,需要增加以下內容:
import PlaygroundSupport
// [...]
PlaygroundPage.current.needsIndefiniteExecution = true
相關問題
- 1. 對Twitter API進行身份驗證
- 2. 如何使用網站API對用戶進行身份驗證?
- 3. 如何使用遠程API對用戶進行身份驗證?
- 4. 如何使用Firebase RESTful API對用戶進行身份驗證?
- 5. 使用node.js進行api身份驗證
- 6. 使用CodeIgniter API進行身份驗證
- 7. 使用REST API進行身份驗證
- 8. 使用Web API進行身份驗證
- 9. 如何使用express對API構建進行身份驗證?
- 10. 如何使用API密鑰對GitHub進行身份驗證?
- 11. 如何使用Python對Bluemix CF API進行身份驗證
- 12. 如何使用redirect_uri(callback.html)對Soundcloud API進行身份驗證
- 13. 無法再對YouTube數據API進行身份驗證
- 14. 如何使用JWT保持使用API進行身份驗證
- 15. 如何使用php和CodeIgniter對Active Directory進行身份驗證?
- 16. 如何使用JNDI和Digest-MD5對LDAP進行身份驗證
- 17. 使用C#API使用SSPI對vSphere進行身份驗證
- 18. 如何通過OIM API對用戶進行身份驗證?
- 19. 解析頁面使用Mechanize進行身份驗證
- 20. 如何在Web API中使用Api密鑰進行使用表單身份驗證的服務身份驗證
- 21. 如何以用戶身份對Azure Active Directory圖形API進行身份驗證?
- 22. 使用ADAL對Azure API應用程序進行身份驗證
- 23. 使用shiro對數據庫進行身份驗證1.2.1
- 24. 如何使用CURL和PHP對Quickbooks Online API進行身份驗證
- 25. Xamarin.Forms Google API使用身份提供商對用戶進行身份驗證
- 26. 如何使用Oracle身份管理器進行身份驗證?
- 27. 如何使用身份驗證令牌對oracle雲ipnetwork API進行身份驗證?
- 28. 如何使用MVC Web API進行身份驗證?
- 29. 如何使用cloudcontrol REST API進行身份驗證
- 30. 如何使用API密鑰進行身份驗證 - Django Tastypie