Search code examples
iosjsonswiftalamofireswifty-json

How do i create and sent a JSON to the server using Alamofire?


I want to sent a JSON to the server,with details of user and options selected by the user. Currently i just save those in simple arrays.

I want to create a JSON and sent it to the server. Normally i can just add each those fields on Parameter with Alamofire. But here i have an issue is that, the number of fields depend on the number of users(or passenger count).

I haven't done this type before and i am a newbie into iOS developing.

{
    "contactPerson": {
        "email": {
            "email": "[email protected]",
            "markedForSendingRezInfo": false,
            "preferred": false,
            "shareMarketInd": false
        },
        "personName": {
            "givenName": "Me",
            "shareMarketInd": false,
            "surname": "Mine"
        },
        "phoneNumber": {
            "countryCode": "93",
            "markedForSendingRezInfo": false,
            "preferred": false,
            "shareMarketInd": false,
            "subscriberNumber": "123456789"
        },
        "shareMarketInd": false,
        "useForInvoicing": false
    },
    "curr": "USD",
    "flight1": "{fligh1Info}",
        "flight2": "{flight2Info}",
    "passengers": [{
        "accompaniedByInfant": true,
        "birthDate": {
            "day": 6,
            "fractionalSecond": 0.000,
            "hour": 0,
            "minute": 0,
            "month": 9,
            "orig_day": 6,
            "orig_fracSeconds": 0.000,
            "orig_hour": 0,
            "orig_minute": 0,
            "orig_month": 9,
            "orig_second": 0,
            "orig_timezone": 330,
            "orig_year": 1991,
            "second": 0,
            "timezone": 330,
            "year": 1991
        },
        "hasStrecher": false,
        "parentSequence": 0,
        "passengerTypeCode": "ADLT",
        "personName": {
            "givenName": "some",
            "nameTitle": "Mr.",
            "shareMarketInd": false,
            "surname": "guy"
        },
        "requestedSeatCount": 1,
        "shareMarketInd": false,
        "unaccompaniedMinor": false
    }, {
        "accompaniedByInfant": false,
        "birthDate": {
            "day": 10,
            "fractionalSecond": 0.000,
            "hour": 0,
            "minute": 0,
            "month": 10,
            "orig_day": 10,
            "orig_fracSeconds": 0.000,
            "orig_hour": 0,
            "orig_minute": 0,
            "orig_month": 10,
            "orig_second": 0,
            "orig_timezone": 330,
            "orig_year": 2010,
            "second": 0,
            "timezone": 330,
            "year": 2010
        },
        "hasStrecher": false,
        "parentSequence": 0,
        "passengerTypeCode": "CHLD",
        "personName": {
            "givenName": "some",
            "shareMarketInd": false,
            "surname": "child"
        },
        "requestedSeatCount": 1,
        "shareMarketInd": false,
        "unaccompaniedMinor": false
    }, {
        "accompaniedByInfant": false,
        "birthDate": {
            "day": 1,
            "fractionalSecond": 0.000,
            "hour": 0,
            "minute": 0,
            "month": 1,
            "orig_day": 1,
            "orig_fracSeconds": 0.000,
            "orig_hour": 0,
            "orig_minute": 0,
            "orig_month": 1,
            "orig_second": 0,
            "orig_timezone": 330,
            "orig_year": 2018,
            "second": 0,
            "timezone": 330,
            "year": 2018
        },
        "hasStrecher": false,
        "parentSequence": 0,
        "passengerTypeCode": "INFT",
        "personName": {
            "givenName": "some",
            "shareMarketInd": false,
            "surname": "kid"
        },
        "requestedSeatCount": 1,
        "shareMarketInd": false,
        "unaccompaniedMinor": false
    }],
    "pwd": "password",
    "requestPurpose": "MODIFY_PERMANENTLY_AND_CALC",
    "unm": "username"
}

This is my JSON. Here number of flight1 flight2 .... depends on number of flights picked by the user.

and number of objects inside passengers depends on number of passengers selected by the user.

How do i do this part?


Solution

  • Try to create a dictionary of [String: Any] type like below example.

    let param : [String: Any] = ["contactPerson": [ "email":[ "email": "[email protected]"], "markedForSendingRezInfo": false], "personName": [], "curr": "USD", "flight1" : [], "flight2" : [] ]
    

    You can add key value in param as per request parameter requirement. then pass this param to alamofire request param.