2 html javascript debugging dojo json
我有JSON数据,我正在尝试使用它来解析它JSON.parse()
.我一直收到错误:unexpected token o
.有人可以帮忙吗?我觉得我应该提一下,我将使用解析的JSON数据来填充Dojo商店,然后将其用于填充Dijit树.有人会推荐任何其他方法来形成树UI吗?
这是我的代码.
$(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;
})),
Run Code Online (Sandbox Code Playgroud)
这是我的Tcl脚本,它在检查输出时提供原始JSON数据,
proc pspTreegetData {} {
set fo [open "C:/Path/To/File/sampleTest.json" r]
set text [read $fo]
close $fo
puts $text
return $text
}
Run Code Online (Sandbox Code Playgroud)
我的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"
}
]
Run Code Online (Sandbox Code Playgroud)
我一直收到错误:意外的令牌o.
这意味着您正在尝试解析已解析的对象.只需删除JSON.parse()
并使用输入数据.
为什么"令牌o"?因为在提供的输入上JSON.parse()
运行toString()
方法以确保它确实是String类型.但是,在对象上调用时toString()
会生成一个字符串"[object Object]"
.第一个字符看起来像数组,但第二个字符绝对不是可解析的.因此错误.
UPD.当您尝试解析jQuery实例对象时,您会非常困惑 - 因为这是您编写时会发生的事情JSON.parse($("#getData").click());
.尽管return state
事件对象内部存在行,但您无法从事件侦听器函数返回,因为它只是没有任何意义.你绑定事件,它可以在5分钟内发生 - 当然脚本不会等到它发生.
我不确定你是如何加载数据的,你只提供了一行:
var state = Function that executes Tcl script and returns the JSON data
Run Code Online (Sandbox Code Playgroud)
但我很确定"执行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
});
})
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
708 次 |
最近记录: |