如何将此Json主体转换为alamofire params格式?

时间:2019-07-06 03:57:19

标签: swift xcode dictionary

我正在执行alamofire请求,并将主体参数转换为快速字典,但我认为我将其转换为错误的格式。谁能建议我将这个json主体转换为快速参数的正确格式是什么?

我在邮递员中发送的实际尸体给出了正确的答复:-

{
    "events": [
        {
            "distance_to_next_route_change": "null",
            "accelerationX": "0.10055954",
            "accelerationY": "-0.0047885496",
            "accelerationZ": "-0.0047885496",
            "drivingState": "DRIVING",
            "latitude": "12.921918",
            "longitude": "77.658917",
            "next_route_coordinate": "null",
            "speed": "33",
            "timeStamp": "1560319632021",
            "tripID": "7090291482_1560319518940",
            "maneuver_list": [],
            "tripState": "null",
            "eventId": "event_tacticurv_id"
        }
    ]
}

我在alamofire参数中发送的内容:-

let params = [
            "events": [
                [
                    "distance_to_next_route_change": "null",
                    "accelerationX": "0.10055954",
                    "accelerationY": "-0.0047885496",
                    "accelerationZ": "-0.0047885496",
                    "drivingState": "DRIVING",
                    "latitude": "12.921918",
                    "longitude": "77.658917",
                    "next_route_coordinate": "null",
                    "speed": "33",
                    "timeStamp": "1560319632021",
                    "tripID": "7090291482_1560319518940",
                    "maneuver_list": [],
                    "tripState": "null",
                    "eventId": "event_tacticurv_id"
                ],
                [
                    "distance_to_next_route_change": "null",
                    "accelerationX": "0.10055954",
                    "accelerationY": "-0.0047885496",
                    "accelerationZ": "-0.0047885496",
                    "drivingState": "DRIVING",
                    "latitude": "12.921918",
                    "longitude": "77.658917",
                    "next_route_coordinate": "null",
                    "speed": "33",
                    "timeStamp": "1560319632021",
                    "tripID": "7090291482_1560319518940",
                    "maneuver_list": [],
                    "tripState": "null",
                    "eventId": "event_tacticurv_id"
                ]
            ]
        ]

预期的居所:

{
    "code": "OK",
    "message": "Successfully processed your request",
    "statusCode": 200
}

回应即将来临

{
    "code": "INTERNAL_SERVER_ERROR",
    "message": "Internal server error occured. Please try again later.",
    "statusCode": 500
}

这是完整的代码:-

func testApi() {

    let defaultManager: Alamofire.SessionManager = {
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "asia-east2-sandboxkruzr.cloudfunctions.net": .pinCertificates(
                certificates: ServerTrustPolicy.certificates(),
                validateCertificateChain: true,
                validateHost: true
            ),
            "35.220.245.158": .disableEvaluation
        ]

        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders

        return Alamofire.SessionManager(
            configuration: configuration,
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
    }()

    let params = [
        "events": [
            [
                "distance_to_next_route_change": "null",
                "accelerationX": "0.10055954",
                "accelerationY": "-0.0047885496",
                "accelerationZ": "-0.0047885496",
                "drivingState": "DRIVING",
                "latitude": "12.921918",
                "longitude": "77.658917",
                "next_route_coordinate": "null",
                "speed": "33",
                "timeStamp": "1560319632021",
                "tripID": "7090291482_1560319518940",
                "maneuver_list": [],
                "tripState": "null",
                "eventId": "event_tacticurv_id"
            ],
            [
                "distance_to_next_route_change": "null",
                "accelerationX": "0.10055954",
                "accelerationY": "-0.0047885496",
                "accelerationZ": "-0.0047885496",
                "drivingState": "DRIVING",
                "latitude": "12.921918",
                "longitude": "77.658917",
                "next_route_coordinate": "null",
                "speed": "33",
                "timeStamp": "1560319632021",
                "tripID": "7090291482_1560319518940",
                "maneuver_list": [],
                "tripState": "null",
                "eventId": "event_tacticurv_id"
            ]
        ]
    ]

    let header = [
        "Content-Type": "application/json",
        "Authorization": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJjMGFhYzBkYy1lZGI5LTQzMzEtYWVjNC0wYmU5NTY3MDQ5M2UifQ.kbKSk5FEu2cWceb9MmZH8wefpyObyjmRM6PRDbJkUW0"
    ]
    defaultManager.request("http://35.220.245.158/gateway/events", method: .post, parameters: params, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
        let _ = defaultManager
        print("api response is")
        print(response.result.value)
    }
}

2 个答案:

答案 0 :(得分:1)

在您的事件中,方括号不正确,需要使用{}来表示单个事件类,而不是表示数组的[]。他们应该看起来像这样。

let params = [
            "events": [
                {
                    "distance_to_next_route_change": "null",
                    "accelerationX": "0.10055954",
                    "accelerationY": "-0.0047885496",
                    "accelerationZ": "-0.0047885496",
                    "drivingState": "DRIVING",
                    "latitude": "12.921918",
                    "longitude": "77.658917",
                    "next_route_coordinate": "null",
                    "speed": "33",
                    "timeStamp": "1560319632021",
                    "tripID": "7090291482_1560319518940",
                    "maneuver_list": [],
                    "tripState": "null",
                    "eventId": "event_tacticurv_id"
                },
                {
                    "distance_to_next_route_change": "null",
                    "accelerationX": "0.10055954",
                    "accelerationY": "-0.0047885496",
                    "accelerationZ": "-0.0047885496",
                    "drivingState": "DRIVING",
                    "latitude": "12.921918",
                    "longitude": "77.658917",
                    "next_route_coordinate": "null",
                    "speed": "33",
                    "timeStamp": "1560319632021",
                    "tripID": "7090291482_1560319518940",
                    "maneuver_list": [],
                    "tripState": "null",
                    "eventId": "event_tacticurv_id"
                }
            ]
        ]

答案 1 :(得分:0)

嗯,我不确定,但是问题似乎不在json不好。 响应代码是500,代表内部服务器错误,但不是400,代表错误请求

我运行了您的代码,它生成了

{
    "speed": "33",
    "accelerationX": "0.10055954",
    "accelerationY": "-0.0047885496",
    "accelerationZ": "-0.0047885496",
    "distance_to_next_route_change": "null",
    "next_route_coordinate": "null",
    "tripID": "7090291482_1560319518940",
    "maneuver_list": [],
    "tripState": "null",
    "timeStamp": "1560319632021",
    "eventId": "event_tacticurv_id",
    "drivingState": "DRIVING",
    "latitude": "12.921918",
    "longitude": "77.658917",
}

要自己检查,只需添加

let request = defaultManager.request("http...
...
debugPrint(request)

这将打印出您的请求的卷曲表示形式