0

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.

6
  • Can you please share the error you are getting from the server when you pass let param : [String: Any] =[ "passengers":passengerparameterarray,"pwd": password,"requestPurpose": "MODIFY_PERMANENTLY_AND_CALC","unm": username ] in the api request. Commented May 31, 2019 at 6:28
  • @AnkushBhatia Its an error telling me that i have an array inside the array and its not supposed to be there. [ [ { "personName" : { "shareMarketInd" : false, "nameTitle" : "MR", "givenName" : "aaaa", "surname" : "aaa" }, "requestedSeatCount" : 1, "gender" : "M", "birthDate" : { "year" : "2019", "day" : "29", "month" : "5" }, "parentSequence" : 0, "passengerTypeCode" : "ADLT", "shareMarketInd" : false } ] ] Commented May 31, 2019 at 6:31
  • 1
    Maybe you should send your Json in the body and not as a parameter Commented May 31, 2019 at 6:31
  • 1
    Also, one passenger should not be stored in an array, it should be stored in a dictionary Commented May 31, 2019 at 6:36
  • 1
    @Zyfe3r Check my answer you need to update JSON parameters in param array Commented May 31, 2019 at 6:39

2 Answers 2

1

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")
Sign up to request clarification or add additional context in comments.

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.