简体   繁体   English

如何编写正确的字符串以使用JSON.parse

[英]How to write proper string to work with JSON.parse

I've got a problem with my node.js application. 我的node.js应用程序出现问题。 I'm trying to parse string using JSON.parse like this: 我正在尝试使用JSON.parse解析字符串,如下所示:

try{
    skills = JSON.parse(user.skills);
    }catch(e){
    console.log(e);
}

In user.skills I've got such a string: 在user.skills中,我有这样一个字符串:

"[ { name: Dreamweaver, level: 60 }, { name: Phototshop, level: 80 }]"

and it throws me: [SyntaxError: Unexpected token n]. 并抛出:[SyntaxError:意外的标记n]。 Can anybody help me? 有谁能够帮助我? Thanks in advance :) 提前致谢 :)

Your JSON data is incorrect. 您的JSON数据不正确。 There should be quotes "" around strings. 字符串周围应带有引号""

should be like this 应该是这样的

var str = "[ { \"name\": \"Dreamweaver\", \"level\": 60 }, { \"name\": \"Phototshop\", \"level\": 80 }]"

If you want to see how should be a proper JSON String Try this 如果您想查看正确的JSON字符串应该如何尝试

var data = {name:"mark"}
JSON.stringify(data) //returns "{"name":"mark"}" Here you have to care about escaping quotes. 

JSON数据需要标识符和值成为字符串,请尝试以下操作:

'[ { "name": "Dreamweaver", "level": "60" }, { "name": "Phototshop", "level": "80" }]';

You can use http://jsonlint.com/ to varify json. 您可以使用http://jsonlint.com/来更改json。 After that we can stringify and parse json respectively. 之后,我们可以分别对json进行字符串化和解析。

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

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