0

客户端.html

我如何在客户端编写这个 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";
            }
        }
    }
}

测试.php

$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
}
4

1 回答 1

0

@Rory,您的函数ID中的问题是您正在迭代对象属性,就好像它是一个数组一样。我想下面的代码可以解决你的问题(你也可以看到它在这个 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);
于 2012-05-18T10:34:39.380 回答