如何从$ .getJSON调用中以print_r()方式打印关联数组?

问题描述:

简单的东西.

$.getJSON并将被调用的.php文件的结果捕获为jquery回调函数中的响应. json编码的php数组是一个关联数组.

$.getJSON is used and the result from the .php file being called is caught as response in the jquery callback function. The json encoded php array is an associative array.

$.getJSON("file.php",function(response){

// print the associative response array in var_dump or print_r fasion

});

file.php包含:

file.php contains:

<?php

$my_array=array();

//$my_array['random_index1']="random_value1";

//$my_array['random_index2']="random_value2";

//$my_array['random_index3']="random_value3";
// and so on


echo json_encode($my_array);


?>

$ my_array具有随机键和值.

$my_array has random keys and values.

我如何打印整个响应"数组,就像php中的print_r时尚一样?

How can I print the whole 'response' array just like the print_r fashion in php?

EDIT1 :我想将其打印在网页上或警报框中.并不是说我只想观看JavaScript控制台中的值(chrome,FF或其他内容).

I want to print it on the webpage or in an alert box. Not that i just want to watch the values in the javascript console(chrome, FF or whatever).

,如果我按如下方式编写$ .getJSON的正文:为什么它不起作用:

if I write the body of $.getJSON as follows: why won't it work:

    for(var i in response){

    console.log("i="+i+" content="+response[i]);

}

我相信console.dir()是您要寻找的:

https://developer.mozilla.org/zh- US/docs/Web/API/Console.dir

唯一的缺点是它不允许标记输出的每个对象,而且它也是非标准的控制台方法.

The only downside is it doesn't allow for labeling each object that's output, and it's also a non-standard console method.