Swift:无法解析JSON数据

时间:2017-06-22 10:51:05

标签: ios json swift parsing

检查时服务工作正常: http://lyrics.wikia.com/api.php?func=getSong&artist=Tom+Waits&song=new+coat+of+paint&fmt=json

但是当我在Swift中尝试它时,我的数据读取“233字节”。

任何人都知道出了什么问题?

这是我的代码:

func getWikiaInfo(completionHandler: @escaping (_ success: Bool?, _ error: NSError?) -> Void) {

    let url = URL(string: "http://lyrics.wikia.com/api.php?func=getSong&artist=Tom+Waits&song=new+coat+of+paint&fmt=json")!
    let request = URLRequest(url: url)

    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in

        // no error, woohoo!
        if error == nil {

            // there was data returned
            if let data = data {

                let parsedResult: [String:AnyObject]!
                do {
                    parsedResult = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:AnyObject]
                } catch {
                    print("Could not parse the data as JSON: '\(data)'")
                    return
                }

                print(parsedResult)
                    completionHandler(true, nil)
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

似乎 API Json数据无法解析。即使在线解析器也无法解析您的API数据。请检查图片下方。

enter image description here enter image description here