I've got a problem with my node.js application. I'm trying to parse string using JSON.parse like this:
try{
skills = JSON.parse(user.skills);
}catch(e){
console.log(e);
}
In user.skills I've got such a string:
"[ { name: Dreamweaver, level: 60 }, { name: Phototshop, level: 80 }]"
and it throws me: [SyntaxError: Unexpected token n]. Can anybody help me? Thanks in advance :)
Your JSON data is incorrect. 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
var data = {name:"mark"}
JSON.stringify(data) //returns "{"name":"mark"}" Here you have to care about escaping quotes.
JSON data needs identifiers and values to be strings, try this:
'[ { "name": "Dreamweaver", "level": "60" }, { "name": "Phototshop", "level": "80" }]';
You can use http://jsonlint.com/ to varify json. After that we can stringify and parse json respectively.