在Swift中使用JSON主体发出HTTP Post请求

时间:2017-02-26 11:57:02

标签: ios json swift3 xcode8

我在swift中很新,并试图发出一个呼叫api的http帖子请求。while it works just fine on postman

这是我需要将后端发布为httpBody的JSON字符串。

myJSON ={"address1":"Mirpur","address2":"D6, f8","cellPhone":"01852540565","city":"fghff","countryName":"Bangladesh","orderDate":"2017-02-25T11:28:24","orderStatus":1,"orderedProductList":[{"discount":0.0,"orderDetailId":0,"price":30000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":{"isBook":false,"typeId":0},"productId":5,"productViews":0},{"discount":0.0,"orderDetailId":0,"price":50000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":{"isBook":false,"typeId":0},"productId":8,"productViews":0},{"discount":0.0,"orderDetailId":0,"price":2000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":{"isBook":false,"typeId":0},"productId":9,"productViews":0}],"paymentTransactionId":"1215455638874521","state":"fyy","zip":"4525","countryId":23,"orderId":0,"orderTotal":82000.0,"paymentMethodId":1,"userId":0}

任何想法如何解决这个问题都会非常有帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

使用@deadbeef建议的语法应该可行,例如重新格式化的myJSON:

    let myJSON: [String : Any] = ["address1":"Mirpur",
                                "address2":"D6, f8",
                                "cellPhone":"01852540565",
                                "city":"fghff",
                                "countryName":"Bangladesh",
                                "orderDate":"2017-02-25T11:28:24",
                                "orderStatus":1,
                                "orderedProductList":[
                                    ["discount":0.0,
                                     "orderDetailId":0,
                                     "price":30000.0,
                                     "quantity":1,
                                     "shippingCharge":50.0,
                                     "supplierId":0,
                                     "tax":0.0,
                                     "type":[
                                        "isBook":false,
                                        "typeId":0
                                        ],
                                     "productId":5,"productViews":0
                                    ],
                                    ["discount":0.0,
                                     "orderDetailId":0,
                                     "price":50000.0,
                                     "quantity":1,
                                     "shippingCharge":50.0,
                                     "supplierId":0,"tax":0.0,
                                     "type":[
                                        "isBook":false,
                                        "typeId":0
                                        ],
                                     "productId":8,
                                     "productViews":0
                                    ],
                                    ["discount":0.0,
                                     "orderDetailId":0,
                                     "price":2000.0,
                                     "quantity":1,
                                     "shippingCharge":50.0,
                                     "supplierId":0,"tax":0.0,
                                     "type":[
                                        "isBook":false,
                                        "typeId":0
                                        ],
                                     "productId":9,
                                     "productViews":0
                                    ]
                                ],
                                "paymentTransactionId":"1215455638874521",
                                "state":"fyy",
                                "zip":"4525",
                                "countryId":23,
                                "orderId":0,
                                "orderTotal":82000.0,
                                "paymentMethodId":1,
                                "userId":0]