如何将PHP数组代码转换为JSON数组


How to transcode a PHP array to a JSON array?

更新:根据这里的建议,我修改了代码。为了了解这是如何工作的,我只是试图用JSON提醒PHP数组。仍然无法使其发挥作用。下面的代码位于同一个test.php页面上。如果刷新,警报是否应该启动?该功能似乎也失败了。TEST警报不起作用。

<?php
  header('Content-type: application/json');
  $arr = array ('item1'=>"test1",'item2'=>"test2",'item3'=>"test3");
  echo json_encode($arr);
?>
<script type="text/javascript">
  $(function() {
    $.getJSON('test.php',function(data) {
      alert("TEST");
      $.each(data, function(key, item) {
        alert(key + item); }); 
      });
    });
  });
</script>

我是JSON的新手,正在尝试在HTML文件中显示PHP数组。下面是一个简单的例子。请告诉我这是否可行,以及我做错了什么。我正在尝试输出此数组中的键。谢谢

我的PHP脚本(test.PHP):

     $arr = array ('item1'=>"test1",'item2'=>"test2",'item3'=>"test3");
json_encode($arr);
$content = file_get_contents('test.htm');
echo $content;

我的HTML页面(test.htm):

<script type="text/javascript">
  $(function() {
$.getJSON('test.php',function(data) {
  $.each(data,function(i, item) {
    alert(item.key);
      });
   });
});
 </script>

它足以:

header('Content-type: application/json');
$arr = array ('item1'=>"test1",'item2'=>"test2",'item3'=>"test3");
echo json_encode($arr);
exit;

然后$.getJSON将获得数据。

确保在HTML文件中包含jQuery。

由于您只是在提醒密钥,所以不必等待dom就绪事件——即使您愿意,我也会首先获取JSON并将$(function...放入ajax回调中。

JSON编码的PHP数组应该如下所示:

{"item1":"test1","item2":"test2","item3":"test3"}

请在调试器中对此进行检查,或者直接在浏览器中加载test.php。正如其他人所建议的,使用file_get_contents的PHP代码有点奇怪。

最后,对象遍历不起作用。查看文档:

$.each(data, function(key, item) {
    alert(key + item);
});

JavaScript中的字符串连接符是+,而不是PHP中的.item.key会尝试访问您的一个项的key属性——没有一个项具有像您的are字符串这样的属性。

如何对调用php页面的文件执行file_get_content()?

您不应该显示它,但应该显示json数据:

$arr = array ('item1'=>"test1",'item2'=>"test2",'item3'=>"test3");
$content = json_encode($arr);
echo $content;

不,这不起作用。

首先,让你的php回应你的数组:

$arr = array('item1'=>"test1", 'item2'=>"test2", 'item3'=>"test3");
echo json_encode($arr);

然后调用javascript ajax函数,观察的响应

<script type="text/javascript">
  $(function() {
$.getJSON('test.php',function(data) {
  $.each(data,function(i, item) {
    alert(item.key);
      });
   });
});
 </script>

这是因为您不需要在php中调用file_get_contents的最后一部分。

在你的php上,你可以只做

$arr = array ('item1'=>"test1",'item2'=>"test2",'item3'=>"test3");
echo json_encode($arr);

在Javascript中,您将返回一个Javascript对象,迭代密钥的一种简单方法是

for(var key in data)
{
    alert(key + ' => ' + data[key]);
}

问题出在您的PHP脚本上

json_encode($arr);

没有做任何事情,变量中没有输出或保存

你从test.htm得到了什么?您不能在JSON中放入两个不同的内容,以便稍后由$.getJSON解析,您的PHP脚本应该只输出JSON编码的

试着在你的PHP代码上这样做:

     $arr = array ('item1'=>"test1",'item2'=>"test2",'item3'=>"test3");
     echo json_encode($arr);

并且在您的Javascript代码上执行此操作之前只需检查:

<script type="text/javascript">
  $(function() {
 $.get("test.php",function(data){
   var obj = $.parseJSON(data);
   alert("Test 1: " + obj.test1);
 });
 </script>

对于那些仍然失败的人:

我使用"$.post"来发送一些数据和检索信息。当使用它时,我的JSON返回就像一个字符串。

解决方案:使用第四个参数dataType作为json

(相应于http://api.jquery.com/jQuery.post/)。

例如:

$.post('http://SOMEWHERE.COM/A/B/C',
   { 
     parameter:value
   },
   function (data) {
     // success
   },
   'json'
);