gpt4 book ai didi

alamofire - JSONSerialization JSON 写入中的类型无效 (_SwiftValue)

转载 作者:行者123 更新时间:2023-12-03 01:12:50 25 4
gpt4 key购买 nike

为什么下面的代码会给我错误:

Invalid type in JSON write (_SwiftValue).

此行抛出错误:

urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters)

完整代码:

let parameters:Parameters = ["resource":[
[
"appUserCode":uuidString,
"productNFCode": self.nfCode!,
"status":code,
"applicationKey":appDelegate.api_key
]
]
]
do {

urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters)
} catch {
// No-op
}

最佳答案

如果这里给出的答案仍然没有解决您的问题。我相信 parameters 内的对象之一可能不是 NSStringNSNumberNSArrayNSDictionary,或NSNull。正如 JSONSerialization 类的文档中给出的:

An object that may be converted to JSON must have the following properties:

  1. The top level object is an NSArray or NSDictionary. All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

  2. All dictionary keys are instances of NSString. Numbers are not NaN or infinity.

  3. Other rules may apply. Calling isValidJSONObject(_:) or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data.

因此,请检查您的 parameters 对象中是否有任何对象不满足上述约束。

关于alamofire - JSONSerialization JSON 写入中的类型无效 (_SwiftValue),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39521430/

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