Mr.G Mr.G - 1 month ago 13
JSON Question

create json array in swift alamofire

Here is the request format that server requires ,

{
"internal_name": "SDSSD",
"display_name": "SDSDSDSD",
"image": "sesse.jpg",
"notes": "sdsdsdsdsdsdsdsd",
"short_description": "ssdsdsd",
"long_description": "sdsdsdsd",
"ean_code": "3434343434",
"status": "not_verified",
"state": "active",
"quantity": 1,
"brand": {
“name”: “My Brand”
},
"categories": [
{
“id”: “My Category”
}
]
}


In here , as you can see , it requires , categories as an array , so my question is how can i create an array . using swift . here is my swift code

let parameters :[String:AnyObject] = [
"internal_name":product.displayName,
"display_name":product.displayName,
"language":Constant.Language.LAN_ENGLISH,
"notes":product.initialName,
"image": product.photo,
"short_description":product.longDescription,
"long_description":product.longDescription,
"ean_code":product.eanCode,
"status":product.status,
"state":Constant.Status.STATUS_ACTIVE,
"categories": [
"id":product.categoryObject.id
],
"quantity":1,

]


this doesnt accept from the server since its not an array , what am i missing here

Answer

Try below code :

let parameters :Parameters = [
        "internal_name":product.displayName,
        "display_name":product.displayName,
        "language":Constant.Language.LAN_ENGLISH,
        "notes":product.initialName,
        "image": product.photo,
        "short_description":product.longDescription,
        "long_description":product.longDescription,
        "ean_code":product.eanCode,
        "status":product.status,
        "state":Constant.Status.STATUS_ACTIVE,
        "brand" : ["name" : "My Brand"],
        "categories": [
            ["id":product.categoryObject.id]
        ],
        "quantity":1,
        ]
Comments