可能重复:
循环通过Json对象
我有一个PHP函数,data.php它从外部服务器URL获取JSON数据,如:
<?php
$url = "https://dev.externalserver.net/directory";
$content = file_get_contents($url);
echo json_encode($content);
?>
Run Code Online (Sandbox Code Playgroud)
检索到的JSON数组如下所示:
[
{ "name": "Not configured",
"mac_address": "1111c11c1111",
"online": "false",
"rate": "Not configured" },
{ "name": "Not configured",
"mac_address": "0000c00c0000",
"online": "false",
"rate": "Not configured" }
]
Run Code Online (Sandbox Code Playgroud)
我现在正在尝试编写一个对该PHP函数的AJAX调用,遍历JSON数组,并以非JSON格式在浏览器中显示它.我的AJAX代码如下所示:
$.ajax({ url: 'data.php',
type: 'POST',
dataType: 'json',
success: function(output) {
$.each(output, function() {
$.each(this, function(key, value){
alert(key + " --> " + value);
});
});
}
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,代码当前显示的警告框,显示像这样的阵列内的单个字符:0 --> [,0 --> ,0 --> …