ios - 如何将 JSON 字符串转换为 Swift 中 POST 请求的正确格式?

标签 ios json swift post type-conversion

我使用大量的字符串插值构建了这个 JSON:

{
    "headers":{
        "email":"email@example.org",
        "rank":0,
        "blue":false,
        "team":1000,
        "round":33,
        "tournament_id":"7F98sdh98aFH98h"
    },
    "data":{
        "start_position":0.0,
        "crossed_line":true,
        "end_platform":true,
        "lift":0,
        "first-actions":[
            {
                "timestamp":1520403299.17746,
                "action":"0_0_1"
            }
        ],
        "second-actions":[
            {
                "timestamp":1520403299.96991,
                "action":"0_0_2"
            }
        ]
    }
}

我试图将其包含在我的 POST 请求的 httpBody 中,如下所示:

request.httpBody = json.data(using: .utf8)

但是,这会导致 422 错误。

服务器端,结果是所有字符串都被解释为单个 header :

--- NEW REQUEST ---
Time: March 6th 2018, 8:47:23 pm (1520398043327)
IP:  [REDACTED]
Request: [REDACTED]
req.body = {'{"headers":{"email":"email@example.org","rank":0,"blue":false,"team":1000,"round":20,"tournament_id":"7F98sdh98aFH98h",},"data":{"start_position":-36.5385,"crossed_line":true,"end_platform":true,"lift":0,"first-actions":[{"timestamp":1520398021.45196,"action":"0_0_1"}],"second-actions":[{"timestamp":1520398022.73314,"action":"0_0_2"}]}}':''}
Auth: [REDACTED]
Auth level: 10

然后我意识到它应该作为 JSON 对象而不是字符串发送。我已经尝试了很多方法来做到这一点,包括将 json 转换为字典,但随后将其转换为数据会出现运行时错误。

我应该如何将字符串转换为正确的格式?

编辑:大卫回答的结果:

--- NEW REQUEST: 60 ---
[REQ 60] Time: March 7th 2018, 8:52:39 pm (1520484759369)
[REQ 60] IP: [REDACTED]
[REQ 60] Request: [REDACTED]
[REQ 60] req.body = { '{"headers":{"team":"1000","email":"email@example.org","rank":"0","blue":"false","round":"22","tournament_id":"7F98sdh98aFH98h"},"data":{"lift":"0","crossed_line":"true","end_platform":"true","first-actions":[{"timestamp":0,"action":"0_0_0"},{"timestamp":1520484747.061681,"action":"0_0_1"}],"second-actions":[{"timestamp":0,"action":"0_0_0"},{"timestamp":1520484747.9255838,"action":"0_0_2"}],"start_position":"0.0"}}': '' }
Auth: [REDACTED]

最佳答案

首先,您不应该使用字符串插值来创建 JSON 对象,而是创建您要发送的实际对象,在您的例子中是 Dictionary .

一旦您将要发送的数据存储在类型为 Dictionary<String,Any> 的变量中,您可以使用 JSONSerialization 将其转换为 JSON或 JSONEncoder API。

let email = "email@example.org"
let dictionary = ["headers":["email":email,"rank":0, "blue":false,"team":1000,"round":33, "tournament_id":"7F98sdh98aFH98h"], "data":[ "start_position":0.0, "crossed_line":true, "end_platform":true, "lift":0, "first-actions":[["timestamp":1520403299.17746,"action":"0_0_1"]],"second-actions":[[ "timestamp":1520403299.96991, "action":"0_0_2"]]]]
do {
    let jsonEncodedDictionary = JSONSerialization.data(withJSONObject: dictionary)
    request.httpBody = jsonEncodedDictionary
} catch {
    //You should handle the errors more appropriately for your specific use case
    print(error)
}

关于ios - 如何将 JSON 字符串转换为 Swift 中 POST 请求的正确格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49161800/

相关文章:

ios - 新模型的架构版本更新

IOS UIView layer setCornerRadius in ellipse 形状

ios - 如何在我的 CocoaTouchFramework 和我的消费应用程序中使用第三方框架?

PHP - json_encode 生成器对象(使用 yield)

java - Spring Rest 操作将响应转换为类

swift - Collection View 单元格不更改图像

ios - 如何让 NSTimer 在强制退出后继续运行

objective-c - ios, UI 行为, 按钮

java - JAX-RS:如何在发送响应之前拦截 json 消息体?

ios - 将 ImageView 放在导航栏上