如何使用PHP的jQuery/AJAX调用迭代JSON数组?(复制)

时间:2022-10-30 23:23:36

Possible Duplicate:
Loop through Json object

可能的重复:通过Json对象循环

I have a PHP function, data.php, which fetches JSON data from an external server URL like such:

我有一个PHP函数,数据。php,从外部服务器URL获取JSON数据,如下所示:

<?php
$url = "https://dev.externalserver.net/directory";
$content = file_get_contents($url);
echo json_encode($content);
?>

The JSON array retrieved looks like the following:

检索到的JSON数组如下所示:

[ 
   { "name": "Not configured", 
     "mac_address": "1111c11c1111", 
     "online": "false", 
     "rate": "Not configured" },

   { "name": "Not configured", 
     "mac_address": "0000c00c0000", 
     "online": "false", 
     "rate": "Not configured" } 
]

I'm now trying to write an AJAX call to that PHP function, iterate through the JSON array, and display it in the browser in non-JSON formatting. My AJAX code looks like the following:

我现在尝试编写一个对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); 
                });
            });
                                   }
});

My issue is that code is currently displaying alert boxes that show the individual characters within the array like such: 0 --> [ , 0 --> , 0 --> { ... etc etc

我的问题是,代码当前显示的是显示数组中单个字符的警告框,如:0—>[,0—>,0—>{…等等

Is there a problem with how I'm passing the data using json_encode(); and dataType: 'json' or does the issue resolve with how I'm iterating through the array?

是否存在使用json_encode()传递数据的问题;和dataType: 'json'或问题解决了我如何遍历数组?

Thanks.

谢谢。

4 个解决方案

#1


6  

Other responders missed the sneaky but still obvious bit, that what's coming back from the resource being polled in the PHP is probably already valid JSON, and re-encoding it is causing the browser to interpret it merely as a string. In this case, the javascript never had a chance.

其他响应器没有注意到,从PHP中轮询的资源返回的可能已经是有效的JSON,重新编码只会导致浏览器将其解释为字符串。在这种情况下,javascript从来没有机会。

Remove the json_encode() bit in the PHP and just echo what comes back, and see if that doesn't improve things.

删除PHP中的json_encode()位,只回显返回的内容,看看这是否没有改进。

#2


2  

I think your problem is the this in the second each. Try:

我认为你的问题是第二个问题。试一试:

$.each(output, function(key, value) { 
    $.each(value, function(key, value){
        alert(key + " --> " + value); 
    });
});

#3


1  

var jsonArray = [ 
     { "name": "Not configured", 
         "mac_address": "1111c11c1111", 
         "online": "false", 
         "rate": "Not configured" },

       { "name": "Not configured", 
         "mac_address": "0000c00c0000", 
         "online": "false", 
         "rate": "Not configured" } 
    ];

    $.each(jsonArray, function() {
      for (var key in this) {
        if (this.hasOwnProperty(key)) {
          console.log(key + " -> " + this[key]);
        }
      }
    });

#4


0  

take a look at this

看看这个

How do I loop through or enumerate a JavaScript object?

如何循环或枚举一个JavaScript对象?

#1


6  

Other responders missed the sneaky but still obvious bit, that what's coming back from the resource being polled in the PHP is probably already valid JSON, and re-encoding it is causing the browser to interpret it merely as a string. In this case, the javascript never had a chance.

其他响应器没有注意到,从PHP中轮询的资源返回的可能已经是有效的JSON,重新编码只会导致浏览器将其解释为字符串。在这种情况下,javascript从来没有机会。

Remove the json_encode() bit in the PHP and just echo what comes back, and see if that doesn't improve things.

删除PHP中的json_encode()位,只回显返回的内容,看看这是否没有改进。

#2


2  

I think your problem is the this in the second each. Try:

我认为你的问题是第二个问题。试一试:

$.each(output, function(key, value) { 
    $.each(value, function(key, value){
        alert(key + " --> " + value); 
    });
});

#3


1  

var jsonArray = [ 
     { "name": "Not configured", 
         "mac_address": "1111c11c1111", 
         "online": "false", 
         "rate": "Not configured" },

       { "name": "Not configured", 
         "mac_address": "0000c00c0000", 
         "online": "false", 
         "rate": "Not configured" } 
    ];

    $.each(jsonArray, function() {
      for (var key in this) {
        if (this.hasOwnProperty(key)) {
          console.log(key + " -> " + this[key]);
        }
      }
    });

#4


0  

take a look at this

看看这个

How do I loop through or enumerate a JavaScript object?

如何循环或枚举一个JavaScript对象?