这是一个艰难的,至少对我而言。基本上我想做的是通过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:我 0:d 等。
----编辑-----
现在修复了,它是“$ .get”的东西。
答案 0 :(得分:3)
这可能是你想要的。
$db->where('user_id', '20');
$results = $db->get('profile_stickers');
$arr = array();
foreach ($results as $parameters => $values) {
$arr[] = $values;
}
echo json_encode($arr);
答案 1 :(得分: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]);
}
}