English 中文(简体)
JSON.parse()的协同效应
原标题:Syntax error from JSON.parse()
  • 时间:2012-04-05 17:20:26
  •  标签:
  • jquery
  • json

我的习惯方法,从扼杀中汲取教训:

function GetJSON(a) {
        if (typeof a !== "string" || !a || a == null) return null;
        a = a.replace(/
|
|
|	/g,   ).replace(/\/g,  / );
        return new Function("return " + a)();
    }

    var notes = {editable:true,useAjax:false,notes:[{"top":76,"left":411,"width":30,"height":30,"text":"hill","editable":true},{"top":183,"left":556,"width":30,"height":30,"text":"lake","editable":true}]} ;

    return GetJSON(notes); //<-- works fine

    //I am trying to replace my custom method with
      return JSON.parse(notes);

But I get syntax error when I call JSON.parse()

什么可能是错误的?

EDIT: I pasted the actual value that I pass to JSON.parse() from debug output.

最佳回答
notes = "{editable:true,useAjax:false,notes:[" + notes + "]}";

你想在这里引用你的关键。 应当:

notes =  {"editable":true,"useAjax":false,"notes":[  + notes +  ]} ;

最后一位专家应当:

var notes = {"editable":true,"useAjax":false,"notes":[{"top":76,"left":411,... 
问题回答

页: 1

应当:

[..snip..] "editable":true},   +  {"top":20,"left"[...snip...]
                           ^^--- missing




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签