为什么 JSON.parse() 不解析这个?
Why won't JSON.parse() parse this?
我有 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;
})),
这是我的 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").click());
时发生的情况。尽管事件对象中有 return state
行,但您 不能从事件侦听器函数中 return,因为它没有任何意义。你绑定事件,它可以在 5 分钟内发生 - 当然脚本不会等到它发生。
我不确定你是如何加载数据的,你只提供了一行:
var state = Function that executes Tcl script and returns the JSON data
但我很确定 "Function that executes Tcl script" 要么接受回调函数,要么接受 returns 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
});
})
});
});
实际上,您似乎遇到了典型的异步问题...
这样的话听起来更合理...
$("#getData").click(function(){
var inputString = "pspTreegetData{}";
var state = Function that executes Tcl script and returns the JSON data
var testStore = new Memory({
data : JSON.parse(state),
//...
检查此 post:
How do I return the response from an asynchronous call?
我有 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;
})),
这是我的 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").click());
时发生的情况。尽管事件对象中有 return state
行,但您 不能从事件侦听器函数中 return,因为它没有任何意义。你绑定事件,它可以在 5 分钟内发生 - 当然脚本不会等到它发生。
我不确定你是如何加载数据的,你只提供了一行:
var state = Function that executes Tcl script and returns the JSON data
但我很确定 "Function that executes Tcl script" 要么接受回调函数,要么接受 returns 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
});
})
});
});
实际上,您似乎遇到了典型的异步问题...
这样的话听起来更合理...
$("#getData").click(function(){
var inputString = "pspTreegetData{}";
var state = Function that executes Tcl script and returns the JSON data
var testStore = new Memory({
data : JSON.parse(state),
//...
检查此 post: How do I return the response from an asynchronous call?