Javascript 用JSON变量填充JsTree
我花了一整天的时间试图让这个东西正常工作,但是不知为什么JsTree不想呈现我的JSON数据 以下是JSON对象的示例:Javascript 用JSON变量填充JsTree,javascript,jquery,json,ajax,jstree,Javascript,Jquery,Json,Ajax,Jstree,我花了一整天的时间试图让这个东西正常工作,但是不知为什么JsTree不想呈现我的JSON数据 以下是JSON对象的示例: {"parent":null, "ProductOption":null, "data":"HlaHd", "text":"global", "DelegationScope":0, "children":null, "entityId":1} 我通过对$(document.ready())的AJAX调用获取JSON对象: 下面是我如何创建树的: function crea
{"parent":null, "ProductOption":null, "data":"HlaHd", "text":"global", "DelegationScope":0, "children":null, "entityId":1}
我通过对$(document.ready()
)的AJAX调用获取JSON对象:
下面是我如何创建树的:
function createJsTree(json) {
$('#ProductTree').jstree({
'core': {
'themes': {
'name': 'proton',
'responsive': true
},
'check_callback': true,
'data': json
}
});
}
起初我认为我的JSON对象可能有问题,所以在创建JsTree之前,我在chrome的控制台上打印了该对象:
function createJsTree(json) {
console.log(json);
$('#ProductTree').jstree({
'core': {
'themes': {
'name': 'proton',
'responsive': true
},
'check_callback': true,
'data': json
}
});
}
JSON对象正是我上面所说的。有趣的是,如果我只是在JsTree创建中粘贴文本JSON对象作为数据,如下所示:
function createJsTree(json) {
$('#ProductTree').jstree({
'core': {
'themes': {
'name': 'proton',
'responsive': true
},
'check_callback': true,
'data': { "parent": null, "ProductOption": null, "data": "HlaHd", "text": "global", "DelegationScope": 0, "children": null, "entityId": 1 }
}
});
}
然后渲染树。这到底是怎么回事?看起来您试图传递一个表示json对象的字符串,而不是对象本身。如果您编写
data:JSON.parse(JSON)
替换data:JSON
它应该可以工作。看起来您正在尝试传递一个表示JSON对象而不是对象本身的字符串。如果您编写data:JSON.parse(JSON)
替换data:JSON
则应该可以使用JSON.parse()
将响应的JSON字符串解析为JSON格式。
希望这会有所帮助 您需要使用
JSON.parse()
将响应的JSON字符串解析为JSON格式。
希望这会有所帮助 天啊,这就解决了。谢谢我快疯了,天哪,这就解决了。谢谢我快疯了。
function createJsTree(json) {
$('#ProductTree').jstree({
'core': {
'themes': {
'name': 'proton',
'responsive': true
},
'check_callback': true,
'data': { "parent": null, "ProductOption": null, "data": "HlaHd", "text": "global", "DelegationScope": 0, "children": null, "entityId": 1 }
}
});
}