0

在php中我有这样的东西:

$i=1; 
foreach ($result as $row){
    $outp['fields'][$i]['id']=$row['id'];
    $outp['fields'][$i]['x']=$row['x'];
    $outp['fields'][$i]['y']=$row['y'];
    $outp['fields'][$i]['type']=$row['type'];
    $i++;
}
echo json_encode($outp)

在 JS 中:

success: function(data) {
  var map = eval('(data)');
}

这是 JSON 输出:

{"fields":
     {"1":{"id":9521,"x":21,"y":96,"type":2},
      "2":{"id":9522,"x":22,"y":96,"type":3},
      "3":{"id":9523,"x":23,"y":96,"type":1},
      "4":{"id":9930,"x":30,"y":100,"type":3}
     } 
}

我想为每个字段(在这种情况下为 4 个字段)编写 html 代码。我该怎么做?这可以将 var map 转换为数组吗?

谢谢

4

1 回答 1

0

尝试这个:

for(var i=1;i<=Object.keys(map["fields"]).length;i++){
    console.log(map["fields"][i]["id"]); //id
    console.log(map["fields"][i]["x"]);  //x
    console.log(map["fields"][i]["y"]);  //y
}
于 2013-03-02T10:51:39.950 回答