无法使用类型的参数列表调用“jsonObject"

编程入门 行业动态 更新时间:2024-10-28 07:32:58
本文介绍了无法使用类型的参数列表调用“jsonObject"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用此代码从具有基本身份验证的服务器获取 JSON 时:

When using this code to get JSON from a server with Basic auth:

let config = URLSessionConfiguration.default let userPasswordData = "\(username!):\(password!)".data(using: .utf8) let base64EncodedCredential = userPasswordData!.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0)) let authString = "Basic \(base64EncodedCredential)" config.httpAdditionalHeaders = ["Authorization" : authString] let session = URLSession(configuration: config) let url = URL(string: "theforest.academy/api/v1/auth") let task = session.dataTask(with: url!) { (data, response, error) in if (response as? HTTPURLResponse) != nil { do { if let data = data { // let json1 = try JSONSerialization.jsonObject(with: data) as? [String: Any] let json = try JSONSerialization.jsonObject(with: data, options: [], error: []) as? [String: Any] if(json["valid"]) { print(json["token"]) } else { print("Invalid login") } } } catch { print("Error deserializing JSON: \(error)") }

我收到以下错误

无法使用类型为(具有:数据,选项:[Any],错误:[Any])'

Cannot invoke 'jsonObject' with an argument list of type '(with: Data, options: [Any], error: [Any])'

推荐答案

查看文档,看来你只有两个选择:

Looking at the documentation, it seems that you only have two choices:

jsonObject(with: Data, options: JSONSerialization.ReadingOptions = [])

jsonObject(with: InputStream, options: JSONSerialization.ReadingOptions = [])

我认为您正在寻找第一种方法.也许你把它与

I think you are looking for the first method. Perhaps you are confusing it with

writeJSONObject(Any, to: OutputStream, options: JSONSerialization.WritingOptions = [], error: NSErrorPointer)

所以,总之,这样的方法是不存在的.这就是您收到错误的原因.

So, in short, such a method does not exist. That is why you are getting the error.

更多推荐

无法使用类型的参数列表调用“jsonObject"

本文发布于:2023-11-26 16:34:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   类型   列表   quot   jsonObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!