如何在客户端编写这个json数据检索,以便遍历每个框及其中的样式。
这样的事情是行不通的......
success: function(output) {
var records = output.data;
var str = "";
if (records) {
alert(records.length);
for (var i = 0; i < records.length; i++) {
for (var j in records[i]) {
str += j + " --> " + records[i][j] + "\n";
}
}
}
}
$sql= "select id, style from table";
$result = mysql_query ($sql);
while($r = mysql_fetch_assoc($result)) {
$id = $r['id'];
$rows[$id] = $r;
}
$data = array(
'data' => $rows,
'debug' => $msg,
'status' => 1
);
在构建功能中借助php转换为json后的数据。 (我想要的格式)
{
"data": {
"box1": { "style":"position: absolute;", "id":"box1" },
"box2": { "style":"position: relative;", "id":"box2" },
"box88": { "style":"position: relative;", "id":"box3" }
},
"debug":"feedback to client end",
"status":1
}
答案 0 :(得分:0)
@Rory,你的函数id中的问题是你正在迭代对象属性,好像它是一个数组。我想以下代码可以解决您的问题(您也可以看到它在this fiddle上工作)。
function process(records) {
result = [];
for(var rec in records) {
var row = [rec + "->"];
for(var stl in records[rec]) {
row.push("\t" + stl + "->" + (records[rec][stl]));
}
result.push(row.join("\n"));
}
$("#result").html(result.join("\n"));
}
// var obj = { your original data }
process(obj.data);