Javascript 在JSON数组中回显变量

Javascript 在JSON数组中回显变量,javascript,php,jquery,Javascript,Php,Jquery,我试图访问并显示页面上json对象中的变量。有人能告诉我为什么变量会显示三次吗 my_array.php <?php $my_data=array(name=>"john",age=>"30", city=>"copenhagen"); // sending output header('Content-Type: text/json'); echo json_encode($my_data,true); ?> <?php $my_data = arr

我试图访问并显示页面上json对象中的变量。有人能告诉我为什么变量会显示三次吗

my_array.php

<?php

$my_data=array(name=>"john",age=>"30", city=>"copenhagen");

// sending output
header('Content-Type: text/json');
echo json_encode($my_data,true);
?>
<?php

$my_data = array(
    name    =>  "john",
    age     =>  "30",
    city    =>  "copenhagen"
);

// sending output
header('Content-Type: application/json');
echo json_encode($my_data, true);

?>
第一件事:

设置:

header("Content-Type: application/json");
以上:

echo json_encode($my_data,true);
在php文件上

或者在javascript上使用以下代码段:

$.getJSON("my_array.php",function(data)
{
  data=JSON.stringify(data);
  $.each(data, function(key) 
  {
    $("#showdata").append(data.city);
  });
});
此外,在上述两种情况下,返回的数据都是一个对象,因此为了在php文件上正确返回,数据必须:

$my_data=array(array(name=>"john",age=>"30", city=>"copenhagen"));
注意:php的json_编码上的关联数组变成了对象。json_encode上的非关联数组仍然是数组

第一件事:

设置:

header("Content-Type: application/json");
以上:

echo json_encode($my_data,true);
在php文件上

或者在javascript上使用以下代码段:

$.getJSON("my_array.php",function(data)
{
  data=JSON.stringify(data);
  $.each(data, function(key) 
  {
    $("#showdata").append(data.city);
  });
});
此外,在上述两种情况下,返回的数据都是一个对象,因此为了在php文件上正确返回,数据必须:

$my_data=array(array(name=>"john",age=>"30", city=>"copenhagen"));

注意:php的json_编码上的关联数组变成了对象。json_encode上的非关联数组仍然是一个数组

,这是因为您正在迭代接收到的json响应中的“每个”数据项,并且my_array.php中有3个键=>值对

删除“$。each(数据,函数(键){}”将只返回一次值'city'

    $(document).ready(function(){
        $("button").click(function(){
            $.getJSON("my_array.php",function(data){
                $("#showdata").append(data.city);
            });
        });
    });

这是因为您正在迭代接收到的json响应中的“每个”数据项,my_array.php中有3个键=>值对

删除“$。each(数据,函数(键){}”将只返回一次值'city'

    $(document).ready(function(){
        $("button").click(function(){
            $.getJSON("my_array.php",function(data){
                $("#showdata").append(data.city);
            });
        });
    });

我猜是因为页面上有三个按钮和$。每个按钮都使用选择器。请尝试:

$("button").click(function(){
  $.getJSON("my_array.php",function(data){
    $("#showdata").append(data.city);
  });
});

我猜是因为页面上有三个按钮和$。每个按钮都使用选择器。请尝试:

$("button").click(function(){
  $.getJSON("my_array.php",function(data){
    $("#showdata").append(data.city);
  });
});
用这个

my_array.php

<?php

$my_data=array(name=>"john",age=>"30", city=>"copenhagen");

// sending output
header('Content-Type: text/json');
echo json_encode($my_data,true);
?>
<?php

$my_data = array(
    name    =>  "john",
    age     =>  "30",
    city    =>  "copenhagen"
);

// sending output
header('Content-Type: application/json');
echo json_encode($my_data, true);

?>

My_page.php

<script>
$(document).ready(function() {
  $("button").click(function() {
    $.getJSON("my_array.php", function(data) {
      $.each(data, function(key) {
        $("#showdata").append(data.city);
      });
    });
  });
});
</script>

//Show the data further down the page.

<div id="showdata"></div>
<div id="showdata"></div>
<button>Click Me!</button>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(e){
        $.getJSON("test.php", function(data){
            console.log(data);
            $("#showdata").append(data.city);
        });
    });
});
</script>

点击我!
$(文档).ready(函数(){
$(“按钮”)。单击(功能(e){
$.getJSON(“test.php”,函数(数据){
控制台日志(数据);
$(“#showdata”).append(data.city);
});
});
});
这只会给你一个哥本哈根

希望它能帮助…

使用这个

my_array.php

<?php

$my_data=array(name=>"john",age=>"30", city=>"copenhagen");

// sending output
header('Content-Type: text/json');
echo json_encode($my_data,true);
?>
<?php

$my_data = array(
    name    =>  "john",
    age     =>  "30",
    city    =>  "copenhagen"
);

// sending output
header('Content-Type: application/json');
echo json_encode($my_data, true);

?>

My_page.php

<script>
$(document).ready(function() {
  $("button").click(function() {
    $.getJSON("my_array.php", function(data) {
      $.each(data, function(key) {
        $("#showdata").append(data.city);
      });
    });
  });
});
</script>

//Show the data further down the page.

<div id="showdata"></div>
<div id="showdata"></div>
<button>Click Me!</button>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(e){
        $.getJSON("test.php", function(data){
            console.log(data);
            $("#showdata").append(data.city);
        });
    });
});
</script>

点击我!
$(文档).ready(函数(){
$(“按钮”)。单击(功能(e){
$.getJSON(“test.php”,函数(数据){
控制台日志(数据);
$(“#showdata”).append(data.city);
});
});
});
这只会给你一个哥本哈根


希望对您有所帮助……

由于JSON对象中有三个键,您正在迭代3次

$.getJSON( "ajax/test.json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    console.log( "key + " " + val" );
  });
查看JQuery文档了解更多信息


由于JSON对象中有三个键,因此需要迭代3次

$.getJSON( "ajax/test.json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    console.log( "key + " " + val" );
  });
查看JQuery文档了解更多信息


Its:
标题(“内容类型:应用程序/json”);
您希望它显示什么?它显示三次,因为您有三个键。Its:
标题(“内容类型:应用程序/json”);
您希望它显示什么?它显示三次,因为您有三个键。