【问题标题】:post array of json with alamofire swift使用 alamofire swift 发布 json 数组
【发布时间】:2023-04-05 05:31:01
【问题描述】:

如何在 swift 中使用 alamofire 发布 json 对象数组?

我的最终数据(我想发布)看起来像:

temp = [{
        "time": 1,
        "score": 20,
        "status": true,
        "answer": 456
    },
    {
        "time": 0,
        "score": 0,
        "status": false,
        "answer": 234
    },
    {
        "time": 0,
        "score": 20,
        "status": true,
        "answer": 123
    }
]

我得到提示,我必须创建自定义参数编码,但我很困惑我该怎么做。有人请帮帮我。

my current code looks like
let parameters: Parameters = [
    "answers": temp,
    "challenge_date": "2019-03-01"
]

Alamofire.request("...url", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
    .responseJSON {
        response in

            if
        let status = response.response ? .statusCode {
            let classFinal: JSON = JSON(response.result.value!)

            if (status > 199 && status < 300) {
                self.dismiss(animated: true)
            } else {


            }
        }

    }

【问题讨论】:

  • 我猜 temp 是你的数组。您遇到的任何错误?
  • 您需要以array & dict的格式发送temp。仔细看,将[ ... ] 视为数组,将{ ... } 视为字典。现在用这个设置 answers 值。 注意 您的 post req 参数数据始终在数组中,而不是 json。
  • @GaneshSomani “JSON 解析错误 - 无法解码 JSON 对象”
  • 您的Parameters 类是如何实现的。这就是你的错误的根源。
  • 请查看已编辑的问题。这是我的最后一个案例。

标签: ios arrays json swift alamofire


【解决方案1】:

在您的代码更改方法 .put.post,而不需要 SVProgressHUD.dismiss() in else,因为您在 if else 部分之前已经关闭

另外,您需要将您的 JSON 字符串(临时变量)转换为数组,然后使用参数传递。

let parameters: Parameters = [
            "answers": temp,
            "challenge_date": "2019-03-01"
        ]

    Alamofire.request("...url", method: .post, parameters: parameters, encoding:  JSONEncoding.default , headers: headers)
        .responseJSON { response in

            if let status = response.response?.statusCode {
            let classFinal : JSON = JSON(response.result.value!)
                SVProgressHUD.dismiss()
                if status > 199 && status < 300 {                    
                     self.dismiss(animated: true)
                }
            }
    }

【讨论】:

  • 是的。仍然收到错误“JSON 解析错误 - 无法解码 JSON 对象”
  • temp是你的JSON字符串吧
  • 先跟邮递员核对一下你的请求和参数,正常吗?
【解决方案2】:

我希望你的Parameters 类遵循Codable 协议。

据我所知,您在将该对象解析为 JSON 时遇到错误。因此,这就是您的错误的根源。

您能否也为您的Parameters 类/结构添加代码

【讨论】:

    【解决方案3】:

    首先,转换你的 Temp

    数组转换成字符串

    而不是将其传入 Alamofire 的参数中。

    extension NSArray {
        
        func toJSonString(data : NSArray) -> String {
            
            var jsonString = "";
            
            do {
                
                let jsonData = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
                jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)! as String
                
            } catch {
                print(error.localizedDescription)
            }
            
            return jsonString;
        }
        
    }
    

    【讨论】:

    • temp 是 json 数组而不是字典
    • 现在传递你的数组并获取字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多