user3383415
user3383415

Reputation: 435

json stringify with variables

I am using JSON stringify, here is my code:

var lista={};
    for(var i=0; i < arr.length; i++){
        lista[i]=[];
        //lista[i].push('imagen:'+arr[i].id);
        lista[i].push('nick:'+arr[i].nick); 
        lista[i].push('tlfno:'+arr[i].tlfno);
        lista[i].push('nombre:'+arr[i].nombre);
        lista[i].push('descripcion:'+arr[i].descripcion);
        lista[i].push('direccion:'+arr[i].direccion);
        lista[i].push('fecha:'+arr[i].fecha);
        lista[i].push('estado:'+arr[i].estado);
        lista[i].push('tipoimagen:'+arr[i].tipoimagen);
        //lista[i].push('imagen:'+arr[i].imagen);
    }
    var json = JSON.stringify(lista);
    console.log(json);

I am getting this output:

{"0":["nick:pepe","tlfno:678909897","nombre:dsfdfsf","descripcion:dsdsdsd","direccion:fdfdf","fecha:fdfdf","estado:1","tipoimagen:image/jpeg"]

but is wrong because it would be like this:

{"0":["nick":"pepe" with double quotes, somebody knows how to show this double quote? Thanks

Upvotes: 0

Views: 927

Answers (1)

adeneo
adeneo

Reputation: 318182

I'm guessing you're trying to push objects into that array, not strings

lista[i].push( {nick: arr[i].nick} );

That would give you something like

{"0":[{"nick":"pepe"}]}

note that {"0":["nick":"pepe"... is not valid, so you won't be getting that

FIDDLE

Upvotes: 3

Related Questions