试图在 alamofire 中发送 json 对象,但它在 json 数组中
Posted
技术标签:
【中文标题】试图在 alamofire 中发送 json 对象,但它在 json 数组中【英文标题】:Trying to send json object in alamofire but it's going in a json array 【发布时间】:2018-04-10 10:21:38 【问题描述】:我希望我的 json 请求是这样的:
"business_id": 1,
"images": [
"image": "iVBORw0KGgoAAAANSUhEUgAAAHgAAACCCAYAAACNbQytAAAACXBIWXMAABcRAAAXEQHKJvM/AAAIJklEQVR4nO2dsY/lRhnAf0tWVKDdLuWtgZoNCaKwsO5dClMgdJvmFWnuBaHX3p6UgoosdKmyoUFnQPdSnoV0G4oUBikvcmQKEnKpoEFv7x9I9iQkKjgKz0uWy+2u57OfPfN5ft3uep5H+3u2P3/zzczWkydPcIG0rD63OPzTjXXkavYbHvfPIom/v9GeNGB76A6cY9fi2Osb60V3vDR0BwC+NnQHApslCFZOEKycIFg5QbBygmDlBMHKCYKVEwQrx6VMlg03Bjz3+wOe2xovBRdJvBzq3GlZDXVqEeEWrZwgWDlBsHKCYOUEwcoZneAoyw+G7kOfjEZwlOW7UZY/BB5EWT4buj99MQrBUZa/AJzyZT3VsfmdetQLNlfrJ8DOuV/vACdRltvUgXmJasFRli+Aexf8+Rpw0l9vhkGl4HPP21tXHHo9yvLjPvo0FOoEP+N5exW3NQdd6gQDS/7/eduEe1qDLo2CJ8J2S41BlzrBq/n0IfCaoOkO9dWvCnWCAVbz6QJ4R9B030TeavBywL8Jq/l0Zp6rTYOtNbeiLF+aL8mz+GW7nvXLlkOzCxt3pEjirSbHmWfqKfZBF8D3zO3ea1Teotes5tMzRh50qRYMIehSLxjGHXSNQjDUQReylQFu+ZzpGo1gwwR4LGjnbaZrVILHGHRtm9GUvr+dD1fz6WHP5wTqoCvK8te4eBjxItZBl1dX8jb1kJp338w2rObTRZTlE64eTnyafTMMOfQ48qTpgV5mstKy2i2S+KzNZ7TIdO0L2gyGr8/grq6gCbKgyxt8FXw9LatF2w9pGXR5ga+CAW6lZTVr+yEtMl1e4JJg0ft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ib65/iLJ8j3p6ba94JxhaXQ0frObTiW0jEzgtsbxzNN0ldZN4+QxezadHyKaMXBcGXdIVfgbHS8EG6bLD0qBLusLPoHgr2DxLxYujSTJdZoUfr/BWMMBqPj1FttfDDnDi416EtngtGMDMtJCs03GN4beo2zjeCwZYzafSdTpEQZdPOLN3oZko1gizus7THFLvDGq71NHtKMsfmr2T1OGMYOwmbX/l/XI1n56ZJIhkcbRjI7mL/SicQsUtek0Iur6KKsEQgq6nUScYWgVdu9quYpWCDbaZrncQlN26jlrBlpmuO6v5dKZNLigWDI2CrsfUy1KofRdWLRguDbo+pR4fXvbaoZ5RLxieGXStn7enw/SoP1xKdGyadaZrofmW/DSjEWwCKKdKWvtgFLfoMRMEKycIVk4QrJwgWDlBsHKCYOV4+R5s5gsFGuClYLrZPm8UhFu0coJg5QTBygmClRMEKycIVk4QrJwgWDlBsHJcymT9BPhOw2OHnCTmVdnP/wA1krVeyFFLQAAAAABJRU5ErkJggg=="
]
即它是一个数组,我希望它是一个 json 对象。
我将我的参数发送为:
let parameter = ["business_id" : business.id , "images" : business.imagearray ] as [String : Any]
这是我发送请求的代码:
Alamofire.request(url, method: HTTPMethod.post, parameters:parameter , encoding: JSONEncoding.default, headers: Request.headers())
将图像附加到图像阵列:
for img in imageArraaay
var dic = Dictionary<String, String>() dic = ["image":imgg] as Dictionary
let jsonData = try! JSONSerialization.data(withJSONObject: myNewDictionary, options: []) as AnyObject
business.imageArray = NSString(data: jsonData as! Data, encoding: String.Encoding.utf8.rawValue)!
imagePicker.dismiss(animated: true, completion: nil)
我知道我犯了一些错误,我是 ios 新手,因此非常感谢任何帮助。
【问题讨论】:
@AbhirajsinhThakore 这与问题有什么关系? 试试这个参数let params = ["business_id": "1", "images":[["image":"base64","image":"base46"]]] as [String : Any]
@phadteviraj 如果您编辑此问题并以正确的格式将其包含在其中会很好
@a.masri 我希望图像数组是动态的,那么如何使用您的方法来完成呢?
@AbhirajsinhThakore 现在可以请您检查一下
【参考方案1】:
这可能对你有用:
let imageArray = ["iVBORw0KGgoAAAANSUhEUg12345","iVBORw0KGgoAAAANSUhEUgAAAHgAAACCCAYAAAC6789"] //Dummy content
print(imageArray)
var ImgParam = [[String:String]]()
for img in imageArray
print(img)
ImgParam.append(["image":img])
print(ImgParam)
let parameter = ["business_id" : 1 , "images" : ImgParam ] as [String : Any]
print(parameter)
【讨论】:
我试过你的方法,但我得到这个错误 responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) ...在编码中我发送 URLEncoding.default 这是我实现的代码 business.id = 1 let parameter = ["business_id" : business.id , "images" : imagePara] as [String : Any] print(parameter) //request Alamofire.request(url, method:.post, parameters: parameter , encoding: URLEncoding.default , 标头:Request.headers()) 您使用的是 POST 或 GET 方法。?【参考方案2】:这是将base64数据附加到imageAarray的逻辑:
var encodedString = String()
let imageData : NSData = UIImageJPEGRepresentation(image, 0) as! NSData
encodedString = imageData.base64EncodedString(options: .lineLength64Characters)
var baseDataDictionary = Dictionary<String , String>()
baseDataDictionary.updateValue(encodedString, forKey: "image")
imagesList.append(baseDataDictionary) // imageList is array of dictionaries
这里我如何传递参数和触发请求:
let parameter = ["business_id" : businessId , "images" : imageList] as [String : Any]
Alamofire.request(url, method:.post, parameters: parameter , encoding: URLEncoding.default, headers: Request.headers())
【讨论】:
以上是关于试图在 alamofire 中发送 json 对象,但它在 json 数组中的主要内容,如果未能解决你的问题,请参考以下文章
使用 Alamofire 请求方法在 POST 的请求正文中发送 JSON 对象数组
使用 Alamofire 3.0+ 在 swift 2.2 中使用 JSON 对象发送 POST 请求
使用 Alamofire 将 JSON 对象作为 post 参数发送