我有一個這樣的對象:如何將JSON轉換爲使用現有方法的JavaScript對象?
function Point(x,y)
{
this.coorX = x;
this.coorY = y;
}
function Node(id,x,y)
{
this.id = id;
this.point = new Point(x,y);
this.getDescription = function(){
return this.id + ': (' + this.punto.coorX + ', ' + this.punto.coorY + ')';
}
}
我JSON格式導出節點的列表,:
JSON.stringify(NodeList);
JSON:
{"NodeList":[{"id":0,"point":{"coorX":15,"coorY":15},"$$hashKey":"004"},{"id":1,"point":{"coorX":15,"coorY":151},"$$hashKey":"009"},{"id":2,"point":{"coorX":25,"coorY":15},"$$hashKey":"00E"}]}
如果我輸入相同的JSON之後附:
NodeList = JSON.parse(text);
導入後如何使用Node
函數getDescription()
?
[JSON(JavaScript的對象符號)是一種輕量級的數據交換格式。](http://www.json.org/) –