Array of the AnyObject type to a Json array using swift

1

I am new to the development of applications for IOS and I am using swift, I need to generate a Json array like this:

{
  "PaymentItems": [
    {
      "id": "1",
      "paid": false
    },
    {
      "id": "2",
      "paid": false
    },
    {
      "id": "3",
      "paid": false
    }
  ]
}

to achieve the above mentioned I am using this block of code, so far without success.

var elementos = [AnyObject]()
for item in transacciones_modificacion
{
  let jsonObjectPaid: [AnyObject]  = [[ "id": item.Id, "paid":  item.Paid]]
elementos.append(jsonObjectPaid)
}    
let pagosArray = ["PaymentItems": [elementos]]

in an arrangement like this:

    
asked by Christian Gonzalez 08.06.2016 в 06:20
source

1 answer

2

You have several misconceptions. To summarize, the correct code would be:

var elementos = [AnyObject]()
for item in transacciones_modificacion
{
    let jsonObjectPaid = ["id": item.Id, "paid": item.Paid]
    elementos.append(jsonObjectPaid)
}
let pagosArray = ["PaymentItems": elementos]
    
answered by 08.06.2016 в 09:00