开发者

Multidimensional array JQUERY and JSON

Ok..so i have a json string (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 to an item in an array.

so, i have

var myArray = new Array();
var parsedJson = $.parseJSON(myJson);
m开发者_如何学PythonyArray['item1']['id'] = parsedJson.id;
myArray['item1']['file'] = parsedJson.file;

but even after the execution of these codes, the length of the array myArray remains zero. Could somebody explain me why this is happening?


Perhaps you're 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 this:

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);


You code can be rewritten simply like this

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

This code will produce the sample result.

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

Please read this for more information about "Objects as associative arrays" http://www.quirksmode.org/js/associative.html


try

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

DEMO


this works perfectly:

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

but it breaks down when i send a multidimensional array created in php, the code i´m using is written 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜