Multidimensional JQUERY and JSON Array

Good. I have a json line (myJson) that looks like this:

{"id": "1", "file": "test.jpg"} 

and in my jquery function, I want to put these id and file values ​​of my json string into an element in an array.

So, I have a

 var myArray = new Array(); var parsedJson = $.parseJSON(myJson); myArray['item1']['id'] = parsedJson.id; myArray['item1']['file'] = parsedJson.file; 

but even after these codes are executed, the length of the myArray array remains zero . Can someone explain to me why this is happening?

+4
source share
4 answers

You might be confusing PHP associative arrays with JavaScript arrays. In JavaScript, you have objects instead of associative arrays, and they behave differently. You can try one of the following approaches depending on your needs:

 var myArray = {}; var parsedJson = $.parseJSON('{"id": "1", "file": "test.jpg"}'); myArray['item1'] = {}; myArray['item1']['id'] = parsedJson.id; myArray['item1']['file'] = parsedJson.file; myArray['item2'] = {}; myArray['item2']['id'] = parsedJson.id + '_2'; myArray['item2']['file'] = parsedJson.file + '_2'; console.log(myArray); 

Or that:

 var myArray = []; var parsedJson = $.parseJSON('{"id": "1", "file": "test.jpg"}'); myArray.push({ id: parsedJson.id, file: parsedJson.file }); myArray.push({ id: parsedJson.id + '_2', file: parsedJson.file + '_2' }); console.log(myArray); 
+4
source

You can rewrite the code this way

 myArray['item1'] = {"id": "1", "file": "test.jpg"}; 

This code will give the result of the selection.

You got the length "myArray" = 0, because in this case, "item1" is a property of the myArray object. This is not a myArray element.

Read this for more information on "Objects as Associative Arrays" http://www.quirksmode.org/js/associative.html

+1
source

to try

 var obj = jQuery.parseJSON('{"id": "1", "file": "test.jpg"}'); alert( obj.id ); alert (obj.file) 

Demo

0
source

this works fine:

 var obj = jQuery.parseJSON('{"id": "1", "file": "test.jpg"}'); alert( obj.id ); alert (obj.file); 

but it breaks when I send a multidimensional array created in php, i'm code is used below:

(Jquery)

 function actualizarIndex(){ /* RECOGEMOS LAS VARIABLES */ $.post('php/consulta-actualizar-index.php', { arriendoConsulta: 'arriendo'} , function(data) { var parsedJson = $.parseJSON(data); alert(parsedJson); alert(parsedJson.tipoInmueble); }).error( function(){ console.log('Error al ejecutar la petición'); } ); } 

(PHP)

 $actArriendo = $_POST["arriendoConsulta"]; //insertamos el inmueble con todas las opciones recbidas $sql = "SELECT * FROM `recomendados-integridad` WHERE `negocio`= '$actArriendo'"; $inmueble = mysql_query($sql, $conexion) or die(mysql_error()); $i = 0; if ($row = mysql_fetch_array($inmueble)){ do { echo "<hr><br>conteo: " . $i ."<br>"; ${'camposInmuebleInicio'.$i} = array( 'tipoInmueble' => $row['tipoInmueble'], 'negocio' => $row['negocio'], 'alcobas' => $row['alcobas'], 'banos' => $row['banos'], ); ++ $i; } while ($row = mysql_fetch_array($inmueble)); } else { echo "¡ No se ha encontrado ningún registro !"; } $casasArriendoArray = array( $camposInmuebleInicio0 , $camposInmuebleInicio1 , $camposInmuebleInicio2); $json = json_encode( $casasArriendoArray ); echo $json; 
0
source

All Articles