Zyfe3r
Zyfe3r

Reputation: 653

How to i insert a JSON object into an array?

I have a JSON

"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",
                "gender":"M" 
        "personName": {
            "givenName": "v",
            "nameTitle": "MR",  
            "shareMarketInd": false,
            "surname": "j"
        },
        "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
    },

and so on depending on the number of passengers.

I have to sent this to the server in

let param : [String: Any] =[ "passengers":passengerparameterarray,"pwd": password,"requestPurpose": "MODIFY_PERMANENTLY_AND_CALC","unm": username ]

Here passengerparameterarray is an array of type string.(here is the issue). I stored details of each passenger in an array paramarray and whenever user finishes adding details, paramarray is added to the passengerparameterarray on position depending on the indexPath. (1 passenger = added on 0th index, 2 = on 0th and 1st index and so on).

But when I send it to server it goes like

"passengers" : [ [ .....  ] ]

This gives me error as I have an array inside of the array. How do I fix this? I try changing to string but that gives me error as String isn't JSON object because of the " coming before { .

I converted each array to JSON Object. But how do i JSON Object the main array? As the Main array is [String]() . If i try changing to [String:Any]() , things like

array.insert()

Wont work.

How do I fix this? I want to add the JSON object into an array and then sent it to the server.

Upvotes: 0

Views: 81

Answers (2)

Zyfe3r
Zyfe3r

Reputation: 653

This was a stupid question. This all happened because originally my array was

var passengerparameterarray = [Array<Any>]()

instead of

var passengerparameterarray = Array<[String: Any]>()

Which as expected stopped me from inserting JSON object which is in the form of "String:Any"

Upvotes: 0

Nikunj Kumbhani
Nikunj Kumbhani

Reputation: 3924

try this

var param : [String:Any] = ["requestedSeatCount" : 1, "gender" : "M", "parentSequence" : 0, "passengerTypeCode" : "ADLT", "shareMarketInd" : false]

param.updateValue(["nameTitle" : "MR", "givenName" : "aaaa", "surname" : "aaa"], forKey: "personName")
param.updateValue(["year" : "2019", "day" : "29", "month" : "5"], forKey: "birthDate")

Upvotes: 1

Related Questions