這是一個艱難的,至少對我而言。所以基本上我想要做的是讓這個JSON數據的每一行的每一個值,通過Javascript:獲取PHP中生成的每行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 等。
---- ----- EDIT
現在修好了,它是「$ .get」的東西。
這不是有效的JSON。 – MightyPork
當從PHP生成JSON時,您只應該一次調用'json_encode' * *!生成你想要的結構,然後在最後調用'json_encode'。 –