0
我正在Swift中進行一個項目(剛剛學習Swift)解析Swift 2中的JSON字符串
並且必須提取並顯示API的JSON輸出。
這裏有API SPEC /例如
API Documentation https://developer.nutritionix.com/v1_1/quick-start/upc-scan
,這裏是到目前爲止的代碼 -
let url = "https://api.nutritionix.com/v1_1/item?upc="
let urlWithUPC = url + barcode + "&appId=[YOURID]&appKey=[YOURKEY]"
print("API Query: "+urlWithUPC)
NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: urlWithUPC)!) { data, response, error in
// Handle result
print("Checked the bar code")
let itemDescription = (((data as NSArray)[0] as? NSDictionary)?["item_description"] as? NSString)
print("Item Description :" + itemDescription)
}.resume()
現在,我只想掃描UPC(其中工程),查找項目(哪些工作),獲取數據(工作),然後提取並顯示所需的部分數據(不工作)。
任何幫助將不勝感激。
謝謝。
'data'是一個'NSData'對象(原始字節),你必須用'NSJSONSerialization'反序列化它。 – vadian
NSJSONSerialization修復了這個問題。謝謝。 –