0

我有一个字符串,我想把它变成一个对象,这样我就可以将它传递给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)

知道我做错了什么吗?

4

1 回答 1

2

JSON 不正确。

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

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

http://jsfiddle.net/eaXjk/

于 2013-07-09T10:21:58.060 回答