Alamofire Swift 5:传递参数时引发错误

问题描述 投票:1回答:1

我的参数值在控制台中打印时看起来像这样,由于我出现错误:

失败(Alamofire.AFError.responseSerializationFailed(原因:Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误:错误域= NSCocoaErrorDomain代码= 3840“周围的无效值字符0。“ UserInfo = {NSDebugDescription =无效值字符0。}))))

["preparation_method[]": "[{\"method_stage\":\"0\",\"method_description\":\"This is test methof1\"}]", "recipe_id": "10454", "ingredients[]": "[{\"ingredient_unit_id\":\"Cup\",\"ingredient_item_id\":\"1\",\"ingredient_item\":\"Rice\",\"ingredient_remark\":\"\",\"ingredient_qty\":\"1\"},{\"ingredient_unit_id\":\"\",\"ingredient_item_id\":\"2\",\"ingredient_item\":\"\",\"ingredient_remark\":\"\",\"ingredient_qty\":\"\"},{\"ingredient_unit_id\":\"\",\"ingredient_item_id\":\"3\",\"ingredient_item\":\"\",\"ingredient_remark\":\"\",\"ingredient_qty\":\"\"}]"]

参数的格式如下:

["recipe_id" : "10454", 
ingredients[]: [{"ingredient_unit_id":"Cup","ingredient_item_id":"1","ingredient_item":"Rice","ingredient_remark":"","ingredient_qty":"1"},
{"ingredient_unit_id":"","ingredient_item_id":"2","ingredient_item":"","ingredient_remark":"","ingredient_qty":""},
{"ingredient_unit_id":"","ingredient_item_id":"3","ingredient_item":"","ingredient_remark":"","ingredient_qty":""}],
 preparation_method[] : 
[{"method_stage":"0","method_description":"This is test methof1"}] ]

我使用的代码是

let arr = ingredient.map({ Model(ingredient_item: $0,
                                  ingredient_item_id: $1,
                                  ingredient_qty: $2,
                                  ingredient_unit_id: $3,
                                  ingredient_remark: $4)})

        let jsonData = try! JSONEncoder().encode(arr)
        let jsonString = String(data: jsonData, encoding: .utf8)
        let ingredientstring = jsonString?.replacingOccurrences(of: "'\'" , with: "")
        let ingredientarrayvalue = "\(ingredientstring!)"
        print(ingredientarrayvalue)
        let ingredient_array = String(ingredientarrayvalue)


        let arr1 = prepatation.map({ Model1(method_stage: $0,
                                  method_description: $1)})

        let jsonData1 = try! JSONEncoder().encode(arr1)
        let jsonString1 = String(data: jsonData1, encoding: .utf8)
        let preparationstring = jsonString1?.replacingOccurrences(of: "'\'" , with: "")
        let preparationarrayvalue = "\(preparationstring!)"
        print(preparationarrayvalue)
        let preparation_array = String(preparationarrayvalue)


        let parameter = ["recipe_id" : recipevalue, "ingredients[]" : ingredient_array, "preparation_method[]" : preparationarrayvalue]
        print(parameter)

 let url = RECIPE.recipeSAVEURL
  AF.request(url, method: .post, parameters: parameter).responseJSON(completionHandler: {response in

            print(response)
            if let response_data = response.value as? NSDictionary{

                let responsestatus = response_data.value(forKey: "response") as! String
                print(responsestatus)

                if responsestatus == "success"{
                }
            }
        })

如何传递参数中不带“ \”的参数?任何人都可以帮我吗?

ios swift parameter-passing alamofire
1个回答
0
投票

在花了数小时寻找上述问题的解决方案之后,我遇到了以下github链接:https://gist.github.com/cprovatas/5c9f51813bc784ef1d7fcbfb89de74fe

他们在其中使用数据扩展名的地方,我在我的代码中将其应用如下:

© www.soinside.com 2019 - 2024. All rights reserved.