How to make a json array: $ in swift 3?

1

Hello, I need help, I do not know how to do a JsonArray in swift 3, the only thing I've achieved is to do this:

{ "Etiqueta":"valor", "":"", "":"" }

and what I need to do is this:

[
   {
      "Etiqueta":"valor",
      "Etiqueta":"valor",
      "Etiqueta":"valor"
   },
   {
      "Etiqueta":"valor",
      "Etiqueta":"valor",
      "Etiqueta":"valor"
   }
]

the code I use to create the json is this:

    var json = [String : AnyObject]()
    var arrayFinish = [String: AnyObject]()

    json["idProducto"] = String(celda) as AnyObject
    json["comentario"] = tfComentario.text! as AnyObject
    json["TipoUnidad"] = pickerselected as String as AnyObject
    json["Cantidad"] = cantidad as String as AnyObject
    json["Medida"] = medidas as String as AnyObject
    json["telefono"] = telefono as String as AnyObject

    arrayFinish["productos"]?.add(json)

and the only thing I get is [:] like this is shown in log

Someone to help me please

    
asked by Luis García 16.09.2017 в 02:22
source

1 answer

0

You are not adding it well, you must give the array already complete.

    var arrayProductos = [[String : AnyObject]]()
    var json = [String : AnyObject]()
    var arrayFinish = [String: AnyObject]()

    json["idProducto"] = String(celda) as AnyObject
    json["comentario"] = tfComentario.text! as AnyObject
    json["TipoUnidad"] = pickerselected as String as AnyObject
    json["Cantidad"] = cantidad as String as AnyObject
    json["Medida"] = medidas as String as AnyObject
    json["telefono"] = telefono as String as AnyObject

    arrayProductos.append(json)

    arrayFinish["productos"] = arrayProductos as AnyObject

And although you're calling it json, you're actually working with dictionaries. If you would like to send it to an API you should use JSONSerialization

    
answered by 18.09.2017 / 11:47
source