我有 JSON 数据,我正在尝试使用 JSON.parse()
解析它。我不断收到错误:意外的 token o
。有人可以帮忙吗?
我觉得我应该提到,我将使用解析的 JSON 数据来填充 Dojo 存储,然后该存储将用于填充 Dijit 树。有人会推荐任何其他方法来形成树状用户界面吗?
这是我的代码。
$(document).ready(function(){
require([
"dojo/ready", "dojo/_base/window", "dojo/json","dojo/dom","dojo/store/Memory", "dojo/store/Observable",
"dijit/tree/ObjectStoreModel", "dijit/Tree", "dojo/domReady!", "dojo/mouse","dojo/text!./data/all.json"],
function(ready, win, JSON, dom, Memory, Observable, ObjectStoreModel, Tree, mouse, data){
var testStore = new Memory({
data : JSON.parse($("#getData").click(
function(){
var inputString = "pspTreegetData{}";
var state = Function that executes Tcl script and returns the JSON data
return state;
})),
这是我的 Tcl 脚本,当我检查其输出时,它会提供原始 JSON 数据,
proc pspTreegetData {} {
set fo [open "C:/Path/To/File/sampleTest.json" r]
set text [read $fo]
close $fo
puts $text
return $text
}
我的Json文件如下,
[
{
"name" : "root",
"id" : "rootNode",
"description" : "This is the root node"
},
{
"name" : "level1_node1",
"id" : "l1n1",
"description" : "This is the first node of level 1",
"parent" : "rootNode"
},
{
"name" : "level2_node1",
"id" : "l2n1",
"description" : "This is the first node of level 2",
"parent" : "l1n1"
},
{
"name" : "level2_node2",
"id" : "l2n2",
"description" : "This is the second node of level 2",
"parent" : "l1n1"
},
{
"name" : "level3_node1",
"id" : "l3n1",
"description" : "This is the first node of level 3",
"parent" : "l2n2"
},
{
"name" : "level3_node2",
"id" : "l3n2",
"description" : "This is the second node of level 3",
"parent" : "l2n2"
},
{
"name" : "level1_node2",
"id" : "l1n2",
"description" : "This is the second node of level 1",
"parent" : "rootNode"
},
{
"name" : "level1_node3",
"id" : "l1n3",
"description" : "This is the third node of level 1",
"parent" : "rootNode"
},
{
"name" : "level2_node3",
"id" : "l2n3",
"description" : "This is the third node of level 2",
"parent" : "l1n3"
},
{
"name" : "level2_node4",
"id" : "l2n4",
"description" : "This is the fourth node of level 2",
"parent" : "l1n3"
},
{
"name" : "level1_node4",
"id" : "l1n4",
"description" : "This is the fourth node of level 1",
"parent" : "rootNode"
}
]
最佳答案
I keep getting the error : unexpected token o.
这意味着您正在尝试解析已经解析的对象。只需删除 JSON.parse()
并按原样使用输入数据即可。
为什么是“token o”?因为 JSON.parse()
在提供的输入上运行 toString()
方法以确保它确实是 String 类型。然而,当在对象上调用toString()
时,会产生一个字符串“[object Object]”
。第一个字符看起来像数组,但第二个字符绝对不可解析。因此出现错误。
UPD。当您尝试解析 jQuery 实例对象时,您会感到非常困惑 - 因为这就是您编写 JSON.parse($("#getData") 时发生的情况。点击());
。尽管事件对象中有返回状态
行,但您无法从事件监听器函数返回,因为它没有任何意义。你绑定(bind)事件,它可以在 5 分钟内发生 - 当然脚本不会等到它发生。
我不确定您到底如何加载数据,您只提供了一行:
var state = Function that executes Tcl script and returns the JSON data
但我很确定“执行 Tcl 脚本的函数”要么接受回调函数,要么返回 thenable/promise 对象。在这种情况下,您的代码应如下所示:
require([
"dojo/ready", "dojo/_base/window", "dojo/json", "dojo/dom", "dojo/store/Memory",
"dojo/store/Observable", "dijit/tree/ObjectStoreModel", "dijit/Tree", "dojo/domReady!",
"dojo/mouse", "dojo/text!./data/all.json"
], function(ready, win, JSON, dom, Memory, Observable, ObjectStoreModel, Tree, mouse, data) {
$("#getData").click(function() {
var inputString = "pspTreegetData{}";
Function_that_executes_Tcl_script(function(data) {
var testStore = new Memory({
data: JSON.parse(data) // or if data is Object already just data: data
});
})
});
});
关于javascript - 为什么 JSON.parse() 不解析这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206713/