I have some data in some arrays in Javascript and I want to go through those arrays and put the values in a Json, I am tested this code and I am not able to create the Json. To check if I have created it I am trying to print a value in
<span id="res"> nada</span>
But I do not know if I'm accessing correctly, I do not control a lot of Javascript.
var arrayNombres= new Array();
var arrayApellido= new Array();
var arrayCiudad= new Array();
arrayNombres[0]= "nombre1";
arrayNombres[1]= "nombre2";
arrayNombres[2]= "nombre3";
arrayApellido[0]= "ape1 ";
arrayApellido[1]= "ape2";
arrayApellido[2]= "ape3";
arrayCiudad[0]= "ciudad1";
arrayCiudad[1]= "ciudad2";
arrayCiudad[2]= "ciudad3";
var json={datos:[{nombre :''},{apellido:''},{ciudad:''}]};
var obj = JSON.parse(json);
for (var i = 0; i < arrayNombres.length; i++) {
obj['datos'].push({"nombre":arrayNombres[i],"apellido":arrayApellido[i],"ciudad":arrayCiudad[i]});
};
json= JSON.stringify(obj);
$("#res").text(''+ json.datos[1].nombre);
The structure I want the Json to have is
{
datos[
{"nombre":"", "apellidos":"", "edad":""}
{"nombre":"", "apellidos":"", "edad":""}
...
]
I have also thought about creating an array with all the speeds I want in the Json and parsing that matrix