为什么JSON.parse()解析了这个?

时间:2015-07-03 12:19:22

标签: javascript html json debugging dojo

我有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"
}
]

2 个答案:

答案 0 :(得分:5)

  

我一直收到错误:意外的令牌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 

但我非常确定"执行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 
            });
        })
    });

});

答案 1 :(得分:0)

实际上看起来你有经典的异步问题......

这样的事情听起来更合理......

$("#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),
 //...

查看此帖子: How do I return the response from an asynchronous call?