使用Alamofire序列化JSON对象 [英] Serializing JSON object with Alamofire

查看:226
本文介绍了使用Alamofire序列化JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化JSON响应,但是在 let json = ...行上出现错误。错误是对成员‘jsonObject(with:options :)’的引用不明确。如果有人知道如何解决此问题,我将予以赞赏

Im trying to run to serialize the JSON response, but I am getting an error on the "let json = ..." line. The error is "Ambiguous reference to member 'jsonObject(with:options:)'". If anyone knows how to fix this I will apprechiate it

    Alamofire.request("https://httpbin.org/get").responseJSON { response in

        if let JSON = response.result.value {

            do {
                let json = try JSONSerialization.jsonObject(with: response.result.value!, options: .allowFragments)

            } catch {
                print ()
            }

            print("JSON: \(JSON)")
        }
    }


推荐答案

因为 response.result.value 是字典 __ NSDictionaryI 的类型,而不是 jsonObject 。您可以轻松地从 JSON 检索值,而无需转换为json,例如: JSON [ title]

Because response.result.value is type of a dictionary __NSDictionaryI, not a Data as jsonObject expected. You can retrieve value from JSON with ease, no need to convert to json, for example: JSON["title"]

这篇关于使用Alamofire序列化JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆