使用HiddenField为Javascript赋予价值

时间:2014-12-09 09:18:42

标签: javascript .net vb.net

var json = {

            "id": "1", "name": "1", "data": "0,3048", "children": [{ "id": "11", "name": "11", "data": "0,3048", "children": [{ "id": "111", "name": "111", "data": "0,3048", "children": [] }, { "id": "112", "name": "112", "data": "0,3048", "children": [{ "id": "1121", "name": "1121", "data": "0,3048", "children": [] }, { "id": "1122", "name": "1122", "data": "0,3048", "children": []}]}] }, { "id": "12", "name": "12", "data": "0,3048", "children": [] }, { "id": "13", "name": "13", "data": "0,3048", "children": [{ "id": "131", "name": "131", "data": "0,3048", "children": [{ "id": "1311", "name": "1311", "data": "0,3048", "children": []}] }, { "id": "132", "name": "132", "data": "0,3048", "children": []}] }, { "id": "14", "name": "14", "data": "0,3048", "children": []}]
        };

如何使用隐藏字段将这些数据提供给json?我的假设是:

JSonID.Value = "{""id"":""1"",""name"":""1"",""data"":""0,3048"",""children"":[{""id"":""11"",""name"":""11"",""data"":""0,3048"",""children"":[{""id"":""111"",""name"":""111"",""data"":""0,3048"",""children"":[]},{""id"":""112"",""name"":""112"",""data"":""0,3048"",""children"":[{""id"":""1121"",""name"":""1121"",""data"":""0,3048"",""children"":[]},{""id"":""1122"",""name"":""1122"",""data"":""0,3048"",""children"":[]}]}]},{""id"":""12"",""name"":""12"",""data"":""0,3048"",""children"":[]},{""id"":""13"",""name"":""13"",""data"":""0,3048"",""children"":[{""id"":""131"",""name"":""131"",""data"":""0,3048"",""children"":[{""id"":""1311"",""name"":""1311"",""data"":""0,3048"",""children"":[]}]},{""id"":""132"",""name"":""132"",""data"":""0,3048"",""children"":[]}]},{""id"":""14"",""name"":""14"",""data"":""0,3048"",""children"":[]}]""}"

var json = document.getElementById('JSonID').value;

它不起作用。 (JSon Id.value是VB代码所以我用“作为转义字符。”

1 个答案:

答案 0 :(得分:0)

var json = document.getElementById('JSonID').value;

将简单地将元素中保存的字符串分配给json变量。您需要将其解析为对象:

var json = JSON.parse(document.getElementById('JSonID').value);

JSON.parse是现代浏览器中的原生方法,但如果您需要支持旧版本,则可以使用json2.js