doter1995 2011-10-05 00:39
浏览 30
已采纳

使用PHP创建JSON提要以便与JQuery一起使用

Edit: Solved using the below suggestion of removing:echo '{"results":'.json_encode($arr).'}'; and replacing with echo json_encode($arr);

This combined with replacing these lines:

var items = [];
        $.each(data, function(key, val) {
            $.each(val, function(key, val)
            {
                    //Test insert into box using customer number
                $("#chatbox").html(val.number);
            });
        });

with

$.each(data, function(key, val) {
$("#chatbox").html(this.number);
});

Made it work, I can now access the object data using the this followed by the property I want.

End Edit

Hi could anyone help me with these JSON feed. I am almost there with it (I think) however whenever I put this feed into JQuery it is picked us as a string rather than as an array of JSON objects and I can't figure out why.

The PHP to create the feed:

<?php
session_start();

include 'connect.php';

    $number = $_SESSION['number'];

    $query = "SELECT * FROM $tableName WHERE number='$number'";

    $result = mysql_query($query, $sqlCon);

    $arr = array();
    while($obj = mysql_fetch_object($result)) {
        $arr[] = $obj;
    }

    if (count($arr) == 0)
    {
        echo "Your reference number is: $number";
    } else {
        echo '{"results":'.json_encode($arr).'}';
    }
?>

The returned JSON looks like this:

{"results":[{"id":"40","number":"466741","message":"dsfdsv","date":"2011-10-05","time":"00:28:32"},{"id":"41","number":"466741","message":"sacsac","date":"2011-10-05","time":"00:30:17"}]}

What I instead want is:

[{"id":"40","number":"466741","message":"dsfdsv","date":"2011-10-05","time":"00:28:32"},{"id":"41","number":"466741","message":"sacsac","date":"2011-10-05","time":"00:30:17"}]

Or a return value which would allow me to iterate over the objects.

The JQuery I'm reading it with:

$.getJSON('get.php', function(data) {

var items = [];
    $.each(data, function(key, val) {
        $.each(val, function(key, val)
        {
                //Test insert into box using customer number
            $("#chatbox").html(val.number);
        });
    });
});

I'm guessing my problem is the way in which I'm creating the JSON feed but I'm just stuck and cant figure out how to fix it.

Any help would be appreciated, thanks.

  • 写回答

1条回答 默认 最新

  • dongluanjie8678 2011-10-05 00:47
    关注

    You should set the headers from PHP to specify that it is json content. This isn't 100% required but good practice and you'll see the content-type parsed correctly in tools like firebug.

    header('Content-type: application/json');
    

    You are wrapping your json content with a results key that that you aren't looking for in your javascript so you either need to remove that part or look for that your javascript. Since you say you want the array and not the object with the results key/value just replace echo '{"results":'.json_encode($arr).'}'; with echo json_encode($arr);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题