简体   繁体   English

jQuery 读取 JSON 数据

[英]jQuery reading JSON Data

So I have a database pass a whole bunch of data using PHP back to jQuery through JSON.. I'm trying to access individual columns from the returned data but no luck..所以我有一个数据库使用 PHP 通过 JSON 将一大堆数据传递回 jQuery.. 我试图从返回的数据中访问各个列,但没有运气..

My PHP:我的PHP:

header('Content-Type: application/json');
require 'database.php';
mysql_query('SET CHARACTER SET utf8');
$myjsons = array();
$qry = 'SELECT * FROM quotes ORDER BY id';
$result = mysql_query($qry);

while($row = mysql_fetch_assoc($result)){ 
  $myjsons[] = json_encode(array($row));
}
echo $_GET['callback'] . '(' . json_encode($myjsons) . ')';

Here's my JS:这是我的JS:

function getAll(){
    jQuery.ajax({
        url:'example.com/js/getAll.php',
        async: true,
        dataType: 'jsonp',
        success:function(data){
            $('.quoteList').append(data[0]);
        }
    });
}

Currently that appends the following to the element:目前将以下内容附加到元素:

[{"id":"1","text":"The most wasted of all days is one without laughter.","author":"E.E. Cummings","status":"1"}]

So for example.. if I wanted the jQuery to go through data[0] to data[92] (the last one) and append the author of each to .quoteList, how could I do that?因此,例如.. 如果我希望 jQuery 通过 data[0] 到 data[92](最后一个)并将每个的作者附加到 .quoteList,我该怎么做? I've tried data[0][1] and data[0][author]我试过 data[0][1] 和 data[0][author]

You can use $.each() to loop through the data and append the author :您可以使用$.each()循环遍历data并附加author

$.each(data, function(i) {
    $('.quoteList').append(data[i]['author']);
});

The PHP might be defective because json_encode is called twice, and this is unusual. PHP 可能有缺陷,因为json_encode被调用了两次,这是不寻常的。 As written this would be flattening the rows into JSON strings, but mere strings nonetheless, which then get JSON encoded again into an array of strings.正如所写的那样,这会将行展平为 JSON 字符串,但仍然只是字符串,然后将 JSON 再次编码为字符串数组。 This is probably not what you intended, as it would be making it possible to print the received data but not access the components of rows which will be decoded to strings and not objects.这可能不是您想要的,因为它可以打印接收到的数据,但不能访问将被解码为字符串而不是对象的行的组件。

Compare https://stackoverflow.com/a/6809069/103081 -- here the PHP echoes back a callback with a single JSON object inside parenthesis () .比较https://stackoverflow.com/a/6809069/103081 - 这里 PHP 用括号()内的单个 JSON 对象回显回调。

I suspect the fix looks like https://stackoverflow.com/a/15511447/103081 and can be adapted as follows:我怀疑该修复程序看起来像https://stackoverflow.com/a/15511447/103081 ,可以进行如下调整:

header('Content-Type: application/json');
require 'database.php';
mysql_query('SET CHARACTER SET utf8');
$myjsons = array();
$qry = 'SELECT * FROM quotes ORDER BY id';
$result = mysql_query($qry);

while($row = mysql_fetch_assoc($result)){ 
  $myjsons[] = $row;
}
echo $_GET['callback'] . '(' . json_encode($myjsons) . ')';

Once you have this, you should be getting back proper JSON for your array of objects and be able to use @Felix's code on the client side.一旦你有了这个,你应该为你的对象数组取回正确的 JSON,并且能够在客户端使用 @Felix 的代码。

you need to use loop on your data, try this你需要在你的数据上使用循环,试试这个

success:function(data){
    for (var i in data) {
        $('.quoteList').append(data[i]);
    }
}

This should work: (upd. all code:)这应该有效:(更新所有代码:)

function getAll(){
    jQuery.ajax({
        url:'example.com/js/getAll.php',
        async: true,
        dataType: 'jsonp',
        contentType: "application/json",
        success:function(data){
            var str = "";

            $(data).each(function(index, item){
                str += item.author + "  ";
            });

            $('.quoteList').append(str);
        }
    });
}

Your problem is here:你的问题在这里:

while($row = mysql_fetch_assoc($result)){ 
  $myjsons[] = json_encode(array($row));
}
echo $_GET['callback'] . '(' . json_encode($myjsons) . ')';

you need something like this:你需要这样的东西:

while($row = mysql_fetch_assoc($result)){ 
  $myjsons[] = $row;
}
$myjsons['callback'] = $_GET['callback'];
echo json_encode($myjsons);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM