1

我正在尝试将我的字符串解析为 JSON 对象,但它不起作用。调试代码时出现语法错误。这是我要解析的字符串:

var listString = "{ title: 'MySchema'," + "root: {id:'"  + headID + "'," 
+   "title:'" + topHead[0].Designation  + "'," + "subtitle:'" + headName + "',";
liststring = liststring + "{ id: '" + head + "'," + "title: '" + childs[cnt].Designation + "'," + "subtitle: '" + title + "'," + "type: '" +  childs[cnt].Typav + "'";

liststring = getChildNodes(tasksEntries, head, liststring); liststring = liststring + "},";}liststring = liststring + "]}}";} listString = childliststring;

$.parseJSON(listString);

我没有得到任何 JSON 对象作为回报。有什么想法吗?

4

6 回答 6

0

您有很多语法错误,其中一个是逗号。'staff'},]}}" 接下来,每个字符串都必须用 " 引号括起来,因此每个名称属性和值。此外,整数值没有引号。

正确的 json 是:

{
    "title":"myTitle",
    "root":{
        "id":1,
        "title":"CEO",
        "subtitle":"John Doe",
        "children":[
            {
                "id":2,
                "title":"COO",
                "subtitle":"Steve Berman",
                "type":"staff"
            },
            {
                "id":3,
                "title":"CTO",
                "subtitle":"Mark Claskov",
                "type":"staff"
            }
        ]
    }
}

你可以在这里测试你的json:http: //json.parser.online.fr/beta/

于 2014-02-18T08:46:22.173 回答
0

首先你忘了;$.parseJSON(myyVar)

var myyVar = {title: 'myTitle',root: {id:'1',title:'CEO',subtitle:'John Doe',children: [{ id: '2',title: 'COO',subtitle: 'Steve Berman',type: 'staff'},{ id: '3',title: 'CTO',subtitle: 'Mark Claskov',type: 'staff'},]}};

其次,您的主要变量myyVar是您必须使用的变量,$.parseJSON()而不是myvar

$.parseJSON(myyVar);

最后,you can correct improper json format

var myyVar = {title: 'myTitle',root: {id:'1',title:'CEO',subtitle:'John Doe',children: [{ id: '2',title: 'COO',subtitle: 'Steve Berman',type: 'staff'},{ id: '3',title: 'CTO',subtitle: 'Mark Claskov',type: 'staff'},]}};
var newVar = JSON.stringify(myyVar );

alert($.parseJSON(newVar));

检查小提琴。[尝试更改alert()console.log()通过浏览器的控制台查看发生了什么]

于 2014-02-18T08:47:05.547 回答
0

您的字符串格式错误。阅读parseJSON API

描述:采用格式良好的 JSON 字符串并返回结果 JavaScript 对象。

添加的版本:1.4.1jQuery.parseJSON( json ) json 类型:String 要解析的 JSON 字符串。

您的 JSON 字符串应采用'{"key":"value"}'格式。传入格式错误的 JSON 字符串会导致引发 JavaScript 异常。例如,以下都是格式错误的 JSON 字符串:

{test: 1} //(test does not have double quotes around it).
{'test': 1} //('test' is using single quotes instead of double quotes).

JSON 标准不允许“控制字符”,例如制表符或换行符。像这样的示例$.parseJSON( '{ "testing":"1\t2\n3" }' )在大多数实现中都会抛出错误,因为 JavaScript 解析器将字符串的制表符和换行符转义为文字制表符和换行符;将反斜杠加倍会 "1\\t2\\n3"产生预期的结果。从服务器端语言(如 PHP)将 JSON 注入 JavaScript 文件时,经常会出现此问题。

于 2014-02-18T08:47:12.547 回答
0

jour JSON 字符串应为 '{"key":"value"}' 格式

JSON.parse("{id:'1'}");

SyntaxError:意外的令牌 i

JSON.parse("{'id':'1'}");

SyntaxError:意外的令牌'

JSON.parse('{"id":"1"}');

对象{id:“1”}

JSON.parse('{"id":1}');

对象 {id: 1}

于 2014-02-18T08:47:28.063 回答
0

使用它来解析 json 字符串。

JSON.parse(json_string);
于 2014-02-18T08:48:26.103 回答
0

你的字符串应该是这样的。

{
"title": "myTitle",
"root": {
    "id": "1",
    "title": "CEO",
    "subtitle": "JohnDoe",
    "children": [
        {
            "id": "2",
            "title": "COO",
            "subtitle": "SteveBerman",
            "type": "staff"
        },
        {
            "id": "3",
            "title": "CTO",
            "subtitle": "MarkClaskov",
            "type": "staff"
        }
      ]
   }
}

您可以在此链接http://jsonlint.com/中测试您的 JSON 是否有效

于 2014-02-18T08:52:39.943 回答