Jason
Jason

Reputation: 33

Sending array json body with arrays inside of it (Alamofire, Swift, iOS)

i want to send json body to my middleware. at first, it works well (note that the "tema" & "emailGroup" wasn't an array). but after some changes on my backend, i have to send this type of json

[{
    "ID": "",
    "Name": "Artikel BU CE - Visit HoB Topic",
    "ChannelType": 0,
    "PublishDate": "2018-09-21T01:00:00Z",
    "Headline": null,
    "Content": null,
    "EmailSubject": null,
    "EmailUrl": null,
    "Mention": null,
    "PostLink": null,
    "ChannelActivityMobileId": null,
    "HashTag": null,
    "Tema": [
        {
            "Value": 6
        }
    ],
    "EmailGroup": [
        {
            "ID": "2c53ea1f-6ebe-e811-a977-000d3aa00fc2",
            "Name": "TV Broadcast",
            "List_EmailListModels": null
        }
    ],
    "ApprovalStatus": 0,
    "ApprovalNote": null,
    "EmployeeId": null,
    "EmployeeLevel": 0
}]

here's my code

let parameters = [["ID" : "", SerializationKeys.channelMobileId : channel.mobileId, SerializationKeys.name : activity.activityName, "ApprovalStatus" : channel.channelStatus, SerializationKeys.channelType : channel.channelType, SerializationKeys.publish_date : channel.publishDate, SerializationKeys.content : channel.content,  SerializationKeys.emailSubject : channel.emailSubject, SerializationKeys.emailURL : channel.emailURL, SerializationKeys.hashtag : channel.hastag, SerializationKeys.mention : channel.mention, SerializationKeys.note : channel.note, SerializationKeys.postLink : channel.postLink, SerializationKeys.tema : [tema]] as [[String : Any]]

where tema is

var tema = [String : Int]()

i got an error that says

[Any] is not convertible to '[[String : Any]]'; did you mean to use as! to force downcast?

and after i change "as" to "as!", it says

Excpected ";" separator

where the semicolon should put near "SerializationKeys.tema : [tema]". Please kindly help me. Thanks

EDIT enter image description here

Upvotes: 1

Views: 288

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

I think you mistakenly added ] near

, SerializationKeys.postLink : channel.postLink],

You may want this

let parameters:[[String:Any]] = [["ID" : "",
                   SerializationKeys.channelMobileId : channel.mobileId,
                   SerializationKeys.name : activity.activityName,
                  "ApprovalStatus" : channel.channelStatus,
                   SerializationKeys.channelType : channel.channelType,
                   SerializationKeys.publish_date : channel.publishDate, 
                   SerializationKeys.content : channel.content,  
                   SerializationKeys.emailSubject : channel.emailSubject,
                   SerializationKeys.emailURL : channel.emailURL,
                   SerializationKeys.hashtag : channel.hastag, 
                   SerializationKeys.mention : channel.mention, 
                   SerializationKeys.note : channel.note, 
                   SerializationKeys.postLink : channel.postLink,
                   SerializationKeys.tema : [tema]
               ]]

Note: I heighly recommend using Codable with struct models for your case

Upvotes: 1

Related Questions