我有一個AJAX功能如下: -如何動態地選擇一個JSON數據節點與jQuery
這樣做是頁面(每個「TD」的songid)上收集的ID列表,將數組發送到我的服務器端腳本,處理所有內容,併爲每個歌曲ID返回包含註釋/回覆的JSON響應。屁股,你可以看到評論的每陣列/回帖是在 'songid-24' 存儲在數組中, 'songid-23' 等...
我的PHP腳本返回以下JSON:
{ "songid-22" : [ ],
"songid-23" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test",
"display_name" : "admin",
"id" : "5",
"playtime" : "56",
"posttime" : "2011-09-28 15:32:41",
"songid" : "23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test comment",
"display_name" : "admin",
"id" : "6",
"playtime" : "87",
"posttime" : "2011-09-28 16:38:37",
"songid" : "23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
],
"songid-24" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "comment at 0:48",
"display_name" : "admin",
"id" : "2",
"playtime" : "48",
"posttime" : "2011-09-28 14:38:41",
"replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "haha reply",
"cid" : "2",
"display_name" : "admin",
"id" : "1",
"posttime" : "2011-09-28 15:14:56",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "next replty",
"cid" : "2",
"display_name" : "admin",
"id" : "2",
"posttime" : "2011-09-28 15:15:07",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "first reply",
"cid" : "2",
"display_name" : "admin",
"id" : "3",
"posttime" : "2011-09-28 15:15:15",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
],
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "asdasd",
"display_name" : "admin",
"id" : "3",
"playtime" : "2",
"posttime" : "2011-09-28 14:43:11",
"replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test reply",
"cid" : "3",
"display_name" : "admin",
"id" : "5",
"posttime" : "2011-10-06 14:35:23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test reply",
"cid" : "3",
"display_name" : "admin",
"id" : "6",
"posttime" : "2011-10-07 14:48:21",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
],
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test",
"display_name" : "admin",
"id" : "10",
"playtime" : "97",
"posttime" : "2011-10-06 14:15:12",
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
],
"songid-25" : [ ],
"songid-26" : [ ],
"songid-27" : [ ],
"songid-28" : [ ],
"songid-29" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "hello hello!",
"display_name" : "admin",
"id" : "24",
"playtime" : "55",
"posttime" : "2011-10-08 10:54:37",
"replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "further testing!",
"cid" : "24",
"display_name" : "admin",
"id" : "12",
"posttime" : "2011-10-08 10:54:48",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "test reply",
"cid" : "24",
"display_name" : "test user 2",
"id" : "13",
"posttime" : "2011-10-08 11:12:42",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
}
],
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "Awesome!",
"display_name" : "admin",
"id" : "25",
"playtime" : "135",
"posttime" : "2011-10-08 10:54:58",
"replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "geggegge",
"cid" : "25",
"display_name" : "test user 2",
"id" : "16",
"posttime" : "2011-10-24 20:04:33",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
} ],
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "testing...",
"display_name" : "test user 2",
"id" : "27",
"playtime" : "35",
"posttime" : "2011-10-08 11:13:01",
"replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "grgrgehhehehe",
"cid" : "27",
"display_name" : "admin",
"id" : "14",
"posttime" : "2011-10-11 15:22:02",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "testing",
"cid" : "27",
"display_name" : "admin",
"id" : "15",
"posttime" : "2011-10-11 18:23:01",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "another",
"cid" : "27",
"display_name" : "admin",
"id" : "20",
"posttime" : "2011-11-25 14:52:02",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
],
"songid" : "29",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "Just testing out the commenting functionality and seeing how good it is now that i\\'ve modified the styling etc...",
"display_name" : "admin",
"id" : "29",
"playtime" : "80",
"posttime" : "2011-10-11 00:46:58",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "hfhhghfhhhhshfdhsh",
"display_name" : "admin",
"id" : "30",
"playtime" : "110",
"posttime" : "2011-10-11 15:21:42",
"replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "thrthhrht",
"cid" : "30",
"display_name" : "admin",
"id" : "19",
"posttime" : "2011-11-10 14:19:03",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
} ],
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "testing......",
"display_name" : "admin",
"id" : "31",
"playtime" : "154",
"posttime" : "2011-10-13 02:10:42",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "hello",
"display_name" : "admin",
"id" : "32",
"playtime" : "94",
"posttime" : "2011-10-13 02:12:27",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "blah",
"display_name" : "admin",
"id" : "33",
"playtime" : "121",
"posttime" : "2011-10-13 02:16:25",
"replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "eeffefef",
"cid" : "33",
"display_name" : "test user 2",
"id" : "17",
"posttime" : "2011-10-24 20:04:48",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
} ],
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "comment",
"display_name" : "admin",
"id" : "34",
"playtime" : "70",
"posttime" : "2011-10-13 02:17:54",
"replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "feffefee",
"cid" : "34",
"display_name" : "test user 2",
"id" : "18",
"posttime" : "2011-10-24 20:04:56",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
} ],
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "commenting",
"display_name" : "admin",
"id" : "35",
"playtime" : "20",
"posttime" : "2011-10-13 02:20:31",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "ffgwgwgwgwgw",
"display_name" : "admin",
"id" : "36",
"playtime" : "102",
"posttime" : "2011-10-13 02:21:30",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "this is a test comment to see if avatars work",
"display_name" : "test user 2",
"id" : "64",
"playtime" : "62",
"posttime" : "2011-10-13 11:46:45",
"songid" : "29",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "comment",
"display_name" : "test user 2",
"id" : "65",
"playtime" : "170",
"posttime" : "2011-10-13 12:21:51",
"songid" : "29",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "rtrgrrg",
"display_name" : "test user 2",
"id" : "66",
"playtime" : "44",
"posttime" : "2011-10-13 13:01:37",
"songid" : "29",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "hello world",
"display_name" : "test user 2",
"id" : "67",
"playtime" : "178",
"posttime" : "2011-10-13 13:44:17",
"songid" : "29",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "comment time!!! YAY!!",
"display_name" : "admin",
"id" : "68",
"playtime" : "48",
"posttime" : "2011-10-13 15:07:56",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "hello world...",
"display_name" : "admin",
"id" : "72",
"playtime" : "86",
"posttime" : "2011-10-13 17:18:58",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "ygygyigyiugyugyuig",
"display_name" : "admin",
"id" : "73",
"playtime" : "28",
"posttime" : "2011-10-14 11:07:27",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test\n",
"display_name" : "admin",
"id" : "74",
"playtime" : "146",
"posttime" : "2011-10-24 19:40:36",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
"body" : "htththth",
"display_name" : "test user 2",
"id" : "76",
"playtime" : "197",
"posttime" : "2011-10-24 20:05:47",
"songid" : "29",
"uid" : "3",
"user_login" : "testing",
"user_url" : "http://www.songbanc.com/members/testing/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "fdgrttgrhrgrgrt",
"display_name" : "admin",
"id" : "77",
"playtime" : "131",
"posttime" : "2011-11-10 14:18:12",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "hello",
"display_name" : "admin",
"id" : "78",
"playtime" : "190",
"posttime" : "2011-11-10 14:18:52",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "testing",
"display_name" : "admin",
"id" : "79",
"playtime" : "117",
"posttime" : "2011-11-17 08:50:38",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "hfdhfdhfdhdhfhhfdh",
"display_name" : "admin",
"id" : "80",
"playtime" : "74",
"posttime" : "2011-11-18 17:29:39",
"songid" : "29",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
],
"songid-30" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "More tests....",
"display_name" : "admin",
"id" : "26",
"playtime" : "126",
"posttime" : "2011-10-08 11:11:55",
"songid" : "30",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "I\\'m just testing the automatic slash stripper \\\"hello\\\", \\'yay\\' !!! it works!!",
"display_name" : "admin",
"id" : "70",
"playtime" : "103",
"posttime" : "2011-10-13 15:51:01",
"songid" : "30",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "Another genius comment....",
"display_name" : "admin",
"id" : "71",
"playtime" : "170",
"posttime" : "2011-10-13 15:55:38",
"songid" : "30",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
]
}
接收到JSON響應後,我試圖執行的操作是通過每個'td'進行迭代,然後向每個模板附加一個jQuery模板。
的問題是,我需要動態選擇數據鍵即data.songid-24,data.songid-23等
在每個循環,我能收集到的ID本身如圖所示代碼爲$ id,因爲我通過使用jquery.data方法將它存儲在每個'td'中。
任何想法?
[如何使用動態密鑰來訪問對象?(HTTP的可能重複:/ /stackoverflow.com/questions/6921803/how-to-access-object-using-dynamic-key) – 2012-01-10 10:46:33
嘗試刪除ID前面的美元符號 – OptimusCrime 2012-01-10 10:46:37
僅供參考,您的問題與JSON無關。響應已經被解析成一個JavaScript對象。 – 2012-01-10 10:47:06