JSON.parse有问题

时间:2013-07-09 09:58:19

标签: json node.js parsing coffeescript mongoose

我有一个字符串,我想把它变成一个对象,所以我可以把它传递给mongoose

string = "{setting: {foo: false}}"

options = JSON.parse(string)

但是这给了我这个错误

SyntaxError: Unexpected token s
    at Object.parse (native)
    at Object.<anonymous> (/Users/home/blah/blah/blah.js:48:20)
    at Object.<anonymous> (/Users/home/blah/blah/blah.js:54:4)
    at Module._compile (module.js:449:26)
    at Object.exports.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:83:25)
    at compileScript (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:177:29)
    at fs.stat.notSources.(anonymous function) (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:152:18)
    at fs.readFile (fs.js:176:14)
    at Object.oncomplete (fs.js:297:15)

知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

JSON不对。

strthing ='{"setting": {"foo": false}}';
options = JSON.parse(strthing);

alert(options.setting.foo);  ----> False.

http://jsfiddle.net/eaXjk/