ios - Swift:无法解析 JSON 数据

标签 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)
                }
            }
        }

最佳答案

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

enter image description here enter image description here

关于ios - Swift:无法解析 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44697267/

相关文章:

iphone - iOS:检测主运行循环之外的低内存崩溃

iphone - 如何在 iOS 中释放 subview

java - 在 Java/Android 中使用 Flexjson 将 JSONObject 映射到列表

ios - 如何减少具有大量项目的 UICollectionView 上的内存使用量?

ios - 如何根据按住 UIButton 的时间来加载 View ?

ios - 不兼容的指针类型用类型为“BaseClass *”的表达式初始化 'SubClass *__strong'

jquery - 自动完成请求/服务器响应是什么样的?

c# - 有没有一种简单的方法可以在 System.Text.Json 的自定义转换器中手动序列化/反序列化子对象?

xcode - Swift 和 Xcode 立即出现错误,无需进行任何更改

ios - 使用 fileNamed 初始化 SKAudioNode 返回 nil