NSJSONSerialization.JSONObjectWithData更改返回结果的内容?

时间:2016-01-08 02:31:29

标签: ios json swift

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返回的数据明显不同?

1 个答案:

答案 0 :(得分:1)

我不相信它实际上在改变价值观,我相信你只是在看一个不同的代表。 (如果Swift的行为类似于Objective-C),print(x)将调用"描述" x上的方法,可以以任何喜欢的格式输出对象。 您可能想要做的是使用以下命令重新编码数据:dataWithJSONObject:options:error(与您所做的基本相反的调用),并查看表示的内容。 https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/dataWithJSONObject:options:error: