Мое значение параметра выглядит так, когда оно печатается в консоли, из-за чего я получаю сообщение об ошибке:
сбой (Alamofire.AFError.responseSerializationFailed (причина: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed (ошибка: Error Domain = NSCocoaErrorDomain Code = 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"{
}
}
})
Как я могу передать параметр без "\" в параметре? Кто-нибудь может мне помочь с этим?