【问题标题】:JSONSerialization returning false in app and true in postmanJSONSerialization 在应用程序中返回 false,在邮递员中返回 true
【发布时间】:2018-12-06 01:55:59
【问题描述】:

我正在使用 Alamofire 和 SwiftyJSON 来发布 HTTP 请求。应用程序中的一个选项允许用户选择一个产品和一个相关的变量选项。我正在使用JSONSerialization 对 JSON 响应进行编码并将其与请求一起发回。

在 Postman 上进行测试时,我得到了肯定的结果,而在应用程序中它返回了错误的结果。我一直在努力寻找解决方案,但一无所获。

发布请求的代码:

   var optionDictionary = [String:AnyObject]()
    var requstParams = [String:String]();
    requstParams["product_id"] = self.productId
    requstParams["quantity"] = self.quantityValue.text
    do {
        let jsonSortData =  try JSONSerialization.data(withJSONObject: self.optionDictionary, options: [])
        let jsonSortString = String(data: jsonSortData, encoding: .utf8)!
        requstParams["option"] = jsonSortString
    }
    catch {
        print(error.localizedDescription)
    }

    NetworkManager.sharedInstance.callingHttpRequest(params:requstParams, apiname:"api/addtoCart", cuurentView: self, method: .post, encoding: JSONEncoding.default){success,responseObject in
        if success == 1{
            let dict = responseObject as! NSDictionary;
            NetworkManager.sharedInstance.dismissLoader()
                self.view.isUserInteractionEnabled = true
                if dict.object(forKey: "success") as! Int == 1{
                    let data = dict.object(forKey: "total") as! String
                    self.tabBarController!.tabBar.items?[3].badgeValue = data.components(separatedBy: " ")[0]
                    self.navigationCart(cartCount:data.components(separatedBy: " ")[0])
                    if self.goToBagFlag == true{
                        self.tabBarController!.selectedIndex = 3
                    }

                }
        }
    }

Xcode 调试器展示

网址https://www.example.com/api/addtoCart
参数 ["product_id": "23490", "quantity": "1", "option": "{\"2008\":\"7404\"}"]
成功 returnData {"success":false,"error":{"option":{"2008":"Option is required!"}}}

当我在 Postman 中使用以下值时

{
"quantity": "1",
"product_id": "23490", 
"option": {"2008":7403}
}

我得到一个成功的 return = true。

我很困惑我做错了什么?

【问题讨论】:

  • 发出 post 请求时您应该检查的另一件事是发送 API 想要的内容。 API 真的需要数字项的字符串吗?
  • 哦!我现在看到了...调试器中的 7404 值是一个字符串。那么你成功地做的是一个 Int。

标签: ios swift alamofire swifty-json nsjsonserialization


【解决方案1】:

好的,伙计……来了……

您的问题与第二行有关:

您正在使用 String 作为值声明您的字典。

var requestParams = [String:String]()

API 需要 Int 作为值。

"option": {"2008":7403}

我建议您停止使用SwiftyJSON,因为Codable 是“工厂”协议。您可以按照希望数据建模的方式对结构进行建模。在这种情况下,这就是您的原始 JSON 作为 Codable 结构的样子。

struct Order: Codable {
    let quantity: String
    let productID: String
    let option: [String: Int]
}

假设您想创建一个订单...您可以这样做:

let order = Order(quantity: "1",
                  productId: "23490",
                  option: ["2008":7403])

当你使用编码器时,你会这样使用它:

let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase

convertToSnakeCase 会将您的 JSON 密钥从 productId 转换为 product_id,并且您的声明将遵循 Swift 的驼峰命名法。

这是post you may find helpful

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 2023-03-26
    • 2015-02-27
    相关资源
    最近更新 更多