【问题标题】:Trouble with JSON.parseJSON.parse 的问题
【发布时间】:2013-07-06 21:20:42
【问题描述】:

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

知道我做错了什么吗?

【问题讨论】:

    标签: json node.js parsing coffeescript mongoose


    【解决方案1】:

    JSON 不正确。

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

    http://jsfiddle.net/eaXjk/

    【讨论】:

    • 对于其他人来说,问题在于我如何使用引号,'"。谢谢@3boll 花了我一分钟才意识到你在说什么。
    猜你喜欢
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2016-05-18
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    相关资源
    最近更新 更多