I'm trying to make a post request with a body in swift using Alamofire.

我的json身体看起来像:

{
    "IdQuiz" : 102,
    "IdUser" : "iosclient",
    "User" : "iosclient",
    "List":[
        {
        "IdQuestion" : 5,
        "IdProposition": 2,
        "Time" : 32
        },
        {
        "IdQuestion" : 4,
        "IdProposition": 3,
        "Time" : 9
        }
    ]
}

我想用NstictionNary制作let list,它看起来像:

[[Time: 30, IdQuestion: 6510, idProposition: 10], [Time: 30, IdQuestion: 8284, idProposition: 10]]

and my request using Alamofire looks like :

Alamofire.request(.POST, "http://myserver.com", parameters: ["IdQuiz":"102","IdUser":"iOSclient","User":"iOSClient","List":list ], encoding: .JSON)
            .response { request, response, data, error in
            let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
                println(dataString)
        }

请求有一个错误,我认为问题在于字典列表,因为如果我在没有列表的情况下发出请求,它会正常工作,所以你知道吗?


I have tried the solution suggested but I'm facing the same problem :

 let json = ["List":list,"IdQuiz":"102","IdUser":"iOSclient","UserInformation":"iOSClient"]
        let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted,error:nil)
        let jsons = NSString(data: data!, encoding: NSUTF8StringEncoding)



    Alamofire.request(.POST, "http://myserver.com", parameters: [:], encoding: .Custom({
        (convertible, params) in
        var mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
        mutableRequest.HTTPBody = jsons!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        return (mutableRequest, nil)
    }))
        .response { request, response, data, error in
        let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
           println(dataString)
    }

推荐答案

你已经很接近了.参数字典格式看起来不正确.您应该try 以下操作:

let parameters: [String: AnyObject] = [
    "IdQuiz" : 102,
    "IdUser" : "iosclient",
    "User" : "iosclient",
    "List": [
        [
            "IdQuestion" : 5,
            "IdProposition": 2,
            "Time" : 32
        ],
        [
            "IdQuestion" : 4,
            "IdProposition": 3,
            "Time" : 9
        ]
    ]
]

Alamofire.request(.POST, "http://myserver.com", parameters: parameters, encoding: .JSON)
    .responseJSON { request, response, JSON, error in
        print(response)
        print(JSON)
        print(error)
    }

Hopefully that fixed your issue. If it doesn't, please reply and I'll adjust my answer accordingly.

Json相关问答推荐

JSON字符串处理注入引号

如何在Haskell中解析JSON,其中字段的名称可以是多个值之一,但应该转换为单个Haskell类型?

无法从MongoDB集合中检索正确的文档

Pandas 对REST API的自定义响应

如何将文件夹组织的.json文件合并为一个JSON文件,文件夹/文件名作为键

如何将一个对象添加到多个对象中 JOLT

如何使用 SQL Server 将 json 存储为字符串的列分解/规范化为行和列?

Scala - 在构建 Json 时无法删除 Key -> value "{}" 大括号的双引号

如何在 Dart 中与多个 map (字典)相交

将 JSON 读取到 pandas 数据框 - ValueError:将 dicts 与非系列混合可能会导致排序不明确

在 Bash 中访问 JSON 对象 - 关联数组/列表/另一个模型

反序列化大型 json 对象的 JsonMaxLength 异常

将 JSON 数据导入 Google 表格

jQuery.getJSON 和 jQuery.parseJSON 返回 [object Object]?

我应该如何处理 JSON 中的 HATEOAS 链接和引用?

未调用 npm package.json 脚本

雅虎财经全币种报价 API 文档

使用 axios 在 POST multipart/form-data 请求中发送文件和 json

我如何反序列化以杰克逊为单位的时间戳?

Javascript对象和JSON对象有什么区别