2017-02-26 125 views
-1

我在swift中很新,試圖發起一個調用api的http post請求。 while it works just fine on postman在Swift中使用JSON正文發出HTTP Post請求

這裏是JSON字符串,我需要作爲httpBody發佈後端。

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} 

任何想法如何解決這個問題,這將是非常有益的。 感謝

+0

字典語法使用'[]'在迅速,不'{}' – deadbeef

+0

在這種情況下,你需要指定變量的類型:'let jsonObject:[String:Any] = ...' – deadbeef

+1

至於提出POST請求,請嘗試[Alamofire](https://github.com/Alamofire/Alamofire) – deadbeef

回答

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]