简体   繁体   English

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

[英]Why won't JSON.parse() parse this?

I have JSON data and I am trying to parse it using JSON.parse() . 我有JSON数据,正在尝试使用JSON.parse()进行解析。 I keep getting the error: unexpected token o . 我不断收到错误: unexpected token o Could someone help? 有人可以帮忙吗? I feel I should mention that I'm going to be using the parsed JSON data to populate a Dojo store which will then be used to populate a Dijit Tree. 我觉得我应该提到我将使用解析的JSON数据填充Dojo存储,然后将其用于填充Dijit树。 Would anyone recommend any other method to form a tree UI? 有人会推荐任何其他方法来形成树形UI吗?

This is my code. 这是我的代码。

$(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;
            })),

This is my Tcl script which gives the raw JSON data when I check its output, 这是我的Tcl脚本,当我检查其输出时会提供原始JSON数据,

proc pspTreegetData {} {
            set fo [open "C:/Path/To/File/sampleTest.json" r]
            set text [read $fo]
            close $fo
            puts $text 
            return $text
}

My Json file is as follows, 我的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. 我不断收到错误:意外令牌o。

It means that you are trying to parse already parsed object. 这意味着您正在尝试解析已解析的对象。 Just remove JSON.parse() and use input data as is. 只需删除JSON.parse()并按原样使用输入数据即可。

Why "token o"? 为什么是“令牌o”? Because JSON.parse() runs toString() method on the provided input to make sure it is indeed of a String type. 因为JSON.parse()在提供的输入上运行toString()方法,以确保它确实是String类型。 However when invoked on the object toString() produces a string "[object Object]" . 但是,在对象上调用toString()会生成字符串"[object Object]" The first character looks like array however the second one is definitely not something parsable. 第一个字符看起来像数组,但是第二个绝对不是可解析的。 Hence the error. 因此,错误。

UPD. UPD。 You are seriously confusing things when you are trying to parse jQuery instance object - because this is what happens when you write JSON.parse($("#getData").click()); 当您尝试解析jQuery实例对象时,您会感到非常困惑-因为在编写JSON.parse($("#getData").click());时会发生这种情况JSON.parse($("#getData").click()); . Despite of the return state line inside event object, you can't return from event listener function, because it's just doesn't make any sense. 尽管事件对象中有return state行,但您无法从事件侦听器函数中返回 ,因为这根本没有任何意义。 You bind event, it can happen in 5 minutes - of course script won't wait until it happens. 您绑定事件,它可能会在5分钟内发生-当然脚本不会等到它发生。

I'm not sure how exactly you load data, you only provided one line: 我不确定您加载数据的方式如何,只提供了一行:

var state =  Function that executes Tcl script and returns the JSON data 

but I'm pretty sure that "Function that executes Tcl script" either accepts callback function or returns thenable/promise object. 但是我很确定“执行Tcl脚本的函数”将接受回调函数或返回thenable / promise对象。 In which case your code should look something like this: 在这种情况下,您的代码应如下所示:

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 
            });
        })
    });

});

Actually it look like you have the classical async issue... 实际上看起来您有经典的异步问题...

Something like that sounds more reasonable... 听起来更合理...

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

Check this post: How do I return the response from an asynchronous call? 检查这篇文章: 如何从异步调用返回响应?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM