func retrieveData(englishWord : String, completionHandler : (success: Bool, data : [String : AnyObject]?, errorString : String?) -> Void) {
let baseURL = "https://api.pearson.com/v2/dictionaries/ldoce5/entries?headword=vodka"
let urlString = baseURL
print(urlString)
let url = NSURL(string: urlString)
let request = NSMutableURLRequest(URL: url!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { data, response, error in
if error != nil {
completionHandler(success: false, data: nil, errorString: "There was a networking error")
return
}
if data == nil {
completionHandler(success: false, data: nil, errorString: "There was an error in the request for data")
return
}
let parsedResult : AnyObject?
do {
parsedResult = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! [String : AnyObject]
print(parsedResult!)
} catch {
completionHandler(success: false, data: nil, errorString: "There was an error in the conversion for data")
return
}
}
task.resume()
}
所以现在print(parsedResult!)
给出了:
{ count = 1; 限制= 10; offset = 0; 结果=( { 数据集=( ldoce5, 字典 ); headword =伏特加; id = cqAG3cXCxZ; “part_of_speech”=名词; 发音=( { audio =( { lang =“英国英语”; type =发音; url =“/ v2 /dictionaries / assets / xdoce / gb_pron / vodka0205.mp3”; } ); ipa =“\ U02c8v \ U0252dk \ U0259”; }, { audio =( { lang =“美式英语”; type =发音; url =“/ v2 / ingaries / assets / ddoce / us_pron / vodka.mp3”; } ); ipa =“\ U02c8v \ U0251 \ U02d0dk \ U0259”; lang =“美式英语”; } ); 感官=( { 定义=( “来自俄罗斯的强烈清澈的酒精饮料,或者一杯这种酒” ); } ); url =“/ v2 / dictionaries / entries / cqAG3cXCxZ”; } ); status = 200; 总数= 1; url =“/ v2 / dictionaries / ldoce5 / entries?headword = vodka”; }
但查询网址为:Here
为方便起见,我将其发布在此处:
{ “状态”:200, “偏移”:0, “限制”:10, “计数”:1, “总”:1, “URL”:“/ V2 /字典/ ldoce5 /条目词条=伏特加”, “结果”:[{ “数据集”:[ “ldoce5”, “字典”], “词条”: “伏特加”, “ID”: “cqAG3cXCxZ”, “part_of_speech”: “名词”, “发音” :[{“audio”:[{“lang”:“British English”,“type”:“发音”,“url”:“/ v2 / dictionaries / assets / ldoce / gb_pron / vodka0205.mp3”}],“ ipa“:”vɒdkə“},{”audio“:[{”lang“:”American English“,”type“:”发音“,”url“:”/ v2 / dictionaries / assets / ldoce / us_pron / vodka。 mp3“}],”ipa“:”vɑːdkə“,”lang“:”美式英语“}},”感官“:[{”定义“:[”一种来自俄罗斯的强烈清澈的酒精饮料,或者一杯这种“]}],” URL “:”/ V2 /字典/条目/ cqAG3cXCxZ“}]}
是否有NSJSONSerialization
更改数据的原因,因为打印出的数据与Pearson API返回的数据明显不同?
答案 0 :(得分:1)