我无法解决在Alamofire
我需要输入如下数据:
[["code": 40, "counts": 10], ["code": 43, "counts": 5], ..]
我在尝试:
let jsonObject = [["code": 40, "counts": 10], ["code": 43, "counts": 5]]
let json = JSON(jsonObject)
let jsonString = json.rawString(.utf8)!
let params = [.."others parameters":.., "order" : jsonString] as [String : Any]
Alamofire.request("http://...", method: .get, parameters: params).responseJSON { response in
而且效果很好
但是这个数组我需要从UserDefaults
开始,我试图这样做:
var ordString = ""
override func viewDidLoad() {
super.viewDidLoad()
if let userArray = UserDefaults.standard.array(forKey: "products") as? [[String : String]] {
var text = "["
for user in userArray {
let quantNumber = user["quantityProd"] ?? ""
let codeNumber = user["codeProd"] ?? ""
text.append("""
["code": \(codeNumber), "counts": \(quantNumber)],
""")
}
text.removeLast(1)
text.append("]")
print(text) . // Give me [["code": 40, "counts": 10], ["code": 43, "counts": 5]]
ordString = text
在Alamofire
:
let jsonObject = ordString
let json = JSON(jsonObject)
let jsonString = json.rawString(.utf8)!
当make请求给我错误JSON INPUT ERROR
答案 0 :(得分:0)
您正在创建字符串,而不是Array
的{{1}}:
key-value pair
我还在最后一个问题上更新了你的错误,并在我的回答中给你一个例子(演示)。请看一下:Swift UserDefaults get values from array