0

这是一个艰难的过程,至少对我来说。所以基本上我想做的是通过Javascript获取这个JSON数据每一行的每一个值:

{"id":2,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":3,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":4,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":5,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":6,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":7,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":8,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":9,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":10,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":31,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":32,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":33,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":34,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":35,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":36,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":37,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":38,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":39,"url":"image.png","x":19,"y":10,"user_id":20}
{"id":40,"url":"image.png","x":19,"y":10,"user_id":20}

我以这种方式生成它(在我的 PHP 文件中):

$db->where('user_id', '20');
$results = $db->get('profile_stickers');

foreach ($results as $parameters => $values) { 
       echo json_encode($values);
}

我已经尝试了很多与 jQuery 相关的代码,$.each但没有运气,代码永远不会工作。我想获得每个值,因为我将在页面上显示它们,并具有正确的位置和图像。有谁知道获取每一行的正确功能?像 PHP 的$row['foo'].

- - - 编辑 - - -

我是这样做的:

$.get('/Application/Ajax/__ajaxProfile.php?a=GetWidgets', function(data) {

var json = data;

for (i=0;i<json.length;i++){
    var obj = json[i];
    for (var key in obj) {
        document.getElementById("log").innerHTML += key+": "+obj[key]+"<br />";
    }
    document.getElementById("log").innerHTML += "<br />";
} 

});

但问题是,它返回的数据如下:

0: " 0: [ 0: { 0: i 0: d 等等。

- - 编辑 - - -

现在修复了,它是“$.get”的东西。

4

2 回答 2

3

这可能是你想要的。

$db->where('user_id', '20');
$results = $db->get('profile_stickers');

$arr = array();

foreach ($results as $parameters => $values) {

    $arr[] = $values;

}

echo json_encode($arr);
于 2013-07-31T18:37:47.193 回答
0

这可能不是您正在寻找的东西,但这是一种在 javascript 中完成所有内容的方法。

您基本上需要将其存储为 JSON 对象数组:

var json = [
{"id":2,"url":"image.png","x":19,"y":10,"user_id":20},
{"id":3,"url":"image.png","x":19,"y":10,"user_id":20},
...etc
]

然后,您可以像这样遍历数组和对象:

for (i=0;i<json.length;i++){
  var obj = json[i];
  for (var key in obj) {
    document.write(key+": "+obj[key]);
  }
}

http://jsfiddle.net/Y4uAx/

于 2013-07-31T18:43:09.817 回答