如何将 JSON 数据放入 CoffeeScript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7375993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I put JSON data into CoffeeScript?
提问by Shamoon
Specifically, if I have some json:
具体来说,如果我有一些 json:
var myData = [ 'some info', 'some more info' ]
var myOtherData = { someInfo: 'some more info' }
What's the correct CoffeeScriptsyntax for that?
什么是正确的CoffeeScript语法?
回答by nicolaskruchten
If you want to create an array you can use myData = ['some info', 'some more info']
如果你想创建一个数组,你可以使用 myData = ['some info', 'some more info']
If you want to create an object you can use myData = {someKey: 'some value'}
如果你想创建一个对象,你可以使用 myData = {someKey: 'some value'}
Or you can use just myData = someKey: 'some value'(i.e. you can omit the {})
或者您可以只使用myData = someKey: 'some value'(即您可以省略{})
For more complicated object structures you use indentation with optional {}and optional commas, for example
对于更复杂的对象结构,您可以使用带有可选{}和可选逗号的缩进,例如
myData =
a: "a string"
b: 0
c:
d: [1,2,3]
e: ["another", "array"]
f: false
will result in the variable myData containing an object with the following JSON representation, (which also happens to be valid CoffeeScript):
将导致变量 myData 包含具有以下 JSON 表示的对象(这也恰好是有效的 CoffeeScript):
{
"a": "a string",
"b": 0,
"c": {
"d": [1, 2, 3],
"e": ["another", "array"]
},
"f": false
}

