0

I have the following multiple json objects to post data using Alamofire. In which format I need to pass the parameters in swift. I am new to swift . Help me out.

{
"refno": "",
"ddesc": "",
"free": "0",
"fgift": "",
"sgift": "",
"sandage": {
               "bank": "",
                "bag": ""
           },
"inst": "",
"items": [{
             "itemid": "606",
             "qty": "1",
    "sub": [{
        "item": "1586",
        "qty": "1",
        "type": "addon",
        "ext": ""
    }, {
        "item": "1588",
        "qty": "1",
        "type": "addon",
        "ext": ""
    }, {
        "item": "1589",
        "qty": "1",
        "type": "addon",
        "ext": ""
    }, {
        "item": "1590",
        "qty": "1",
        "type": "addon",
        "ext": ""
    }]
}, {
    "itemid": "639",
    "qty": "1",
    "sub": [{
        "item": "1618",
        "qty": "1",
        "type": "addon",
        "ext": ""
    }, {
        "item": "1612",
        "qty": "1",
        "type": "addon",
        "ext": ""
    }, {
        "item": "1611",
        "qty": "1",
        "type": "addon",
        "ext": ""
    }, {
        "item": "1610",
        "qty": "1",
        "type": "addon",
        "ext": ""
    }]
}],
"discount": "0",
"coupon": [],
"delivery": "",
"user": {
    "id": "13",
    "fname": "Demo",
    "lname": "Order",
    "phone": "9876543210",
    "dno": "",
    "add1": "",
    "add2": "",
    "postcode": "",
    "username": "demo@theepos.com",
    "status": "1"
},
"otype": "1",
"ptype": "0",
"app_id": "A1A2A3A4", 
 "app_key": "K1K2K3K4",                   
"request": "placeorder"
}

I am using Alamofire to post the data..

 var url: NSURL = NSURL(string: "http://\(platform).eposapi.co.uk")!
    let params = [
        "refno": "",
        "ddesc": "",
        "free": "0",
        "fgift": "",
        "sgift": "",
        .....
    ]

    Alamofire.request(.POST, url, parameters: params, encoding: .JSON)
        .responseJSON{ response in

            if let result: AnyObject = response.result.value
                {

                    let post: JSON = JSON(result)

            }

        }
PRADIP KUMAR
  • 489
  • 1
  • 9
  • 31
  • you can find it here http://stackoverflow.com/questions/27026916/sending-json-array-via-alamofire – Gandharv Sep 15 '16 at 11:40
  • In Alamofire I know How to post it.. My problem is In which format i should pass the parameters. because there are multiple nested objects as well as arrays in json format..@Gandharv – PRADIP KUMAR Sep 15 '16 at 11:45
  • It might [help](https://github.com/Alamofire/Alamofire#post-request-with-url-encoded-parameters) you. – Zico Sep 15 '16 at 12:27
  • 1
    There are using `/// A dictionary of parameters to apply to a URLRequest. public typealias Parameters = [String: Any]` for the parameters. – Zico Sep 15 '16 at 12:29
  • 1
    serialize data and post as json – Gandharv Sep 15 '16 at 12:52

1 Answers1

1

Really thanks guys . I only followed the above commented instructions and got the answer. Help really appreciated

    var url: NSURL = NSURL(string: "http://\(platform).eposapi.co.uk")!

    let sandage = ["bank": "","bag": ""]
    let sub_array = [ "item": "1586","qty": "1","type": "addon","ext": ""]
    let items_array = ["itemid": "606","qty": "1","sub": sub_array ]
    let user_Detail = ["id": "13","fname": "Demo","lname": "Order",
                       "phone": "9876543210","dno": "","add1": "",
                       "add2": "","postcode": "","username": "demo@theepos.com",
                       "status": "1"]

    let params = [
        "refno": "",
        "ddesc": "",
        "free": "0",
        "fgift": "",
        "sgift": "",
        "sandage": sandage,
        "inst": "",
        "items":items_array,
        "discount": "0",
        "coupon": [],
        "delivery": "",
        "user": user_Detail,
        "otype": "1",
        "ptype": "0",
        "app_id": "A1A2A3A4",
        "app_key": "K1K2K3K4",
        "request": "placeorder"
    ]


    Alamofire.request(.POST, url, parameters: params, encoding: .JSON)
        .responseJSON{ response in

            if let result: AnyObject = response.result.value
                {

                    let post: JSON = JSON(result)

                    let status = post["status"].stringValue
                    let order_id = post["order_id"].stringValue
                    print(status)
                    print(order_id)
PRADIP KUMAR
  • 489
  • 1
  • 9
  • 31