func searchBuscape(query: String) {
guard
let escapedSearch = query.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()),
url = NSURL(string: "http://sandbox.buscape.com.br/service/findProductList/554163674d2f57624d676f3d/BR/?categoryId=77&keyword=\(escapedSearch)&format=json")
else { return }
NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
guard
let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
let data = data where error == nil
else { return }
dispatch_async(dispatch_get_main_queue()) { () -> Void in
var error: NSError?
let json = JSON(data: data, options: .AllowFragments, error: &error)
if let error = error {
print(error.localizedDescription)
}
print("===json start")
print(json)
print("===json end")
print(json["totalresultsreturned"]) // 16
print(json["product"][0]["product"]["pricemin"]) // 819.90
print(json["product"][0]["product"]["pricemax"]) // 1199.00
// pra voce extrair um array de dicionários do json object você precisa acessar arrayObject property da segunte forma
if let products = json["product"].arrayObject as? [[String:AnyObject]] {
for product in products {
print("productStart=======")
print(product)
print("productEnd=======")
}
let pricesArrayMin = products.map{$0["product"]?["pricemin"]??.doubleValue ?? 0}.sort()
print("pricesMinStart=======")
print(pricesArrayMin)
print(pricesArrayMin.count) // 16
print(pricesArrayMin.first!) // 539.1 menor(produto precomais minimobarato)
print(pricesArrayMin.last!) // 2898.99
print("pricesMinEnd=======")
}
}
}).resume()
}