gpt4 book ai didi

json - 从 JSON 解码为自定义对象

转载 作者:行者123 更新时间:2023-11-30 11:21:07 25 4
gpt4 key购买 nike

我正在尝试从 JSON 进行解码(我使用 swiftyJSON 和 Alamofire 来处理我的 HTTP 请求)。swiftyJsonVar 是我的 JSON 对象

let decoder = JSONDecoder()**

let user = try! decoder.decode([User.self], for:(swiftyJsonVar))**

写这个时我有以下错误

incorrect argument label in call (have ':for:', expected ':from:'

因此,当我切换到“from:”时,出现以下错误:

Cannot convert value of type 'JSON' to expected argument type 'Data'* "

当我将从请求中获取的数据值转换为 JSON 之前,它工作正常,但当响应是 JSONArray 时,我不能再这样做了。感谢您的帮助。

最佳答案

错误“无法将类型‘JSON’的值转换为预期参数类型‘数据’”清楚地说明了问题所在。

“from”参数的数据类型需要是Data,因此需要转换swiftyJsonVar。如果 swiftJsonVar 是 SwiftyJSON 对象,则类似于 let data = try swiftyJsonVar.rawData()

关于json - 从 JSON 解码为自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51260084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com