YAML 等效于 JSON 中的对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33989612/
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
YAML equivalent of array of objects in JSON
提问by wegry
I have a JSON array of objects that I'm trying to convert to YAML.
我有一个 JSON 对象数组,我试图将其转换为 YAML。
{"AAPL": [
{
"shares": -75.088,
"date": "11/27/2015"
},
{
"shares": 75.088,
"date": "11/26/2015"
},
]}
Is there an equivalent representation in YAML that isn't just JSON? I'd like to do something like
YAML 中是否有不只是 JSON 的等效表示?我想做类似的事情
AAPL:
- :
shares: -75.088
date: 11/27/2015
- :
shares: 75.088
date: 11/26/2015
but the cleanest thing I've come up with is
但我想出的最干净的东西是
AAPL:
- {
shares: -75.088,
date: 11/27/2015
}
{
shares: 75.088,
date: 11/26/2015
}
回答by Jordan Running
TL;DR
TL; 博士
You want this:
你要这个:
AAPL:
- shares: -75.088
date: 11/27/2015
- shares: 75.088
date: 11/26/2015
Mappings
映射
The YAML equivalent of a JSON object is a mapping, which looks like these:
JSON 对象的 YAML 等价物是一个映射,如下所示:
# flow style
{ foo: 1, bar: 2 }
# block style
foo: 1
bar: 2
Note that the first characters of the keys in a block mapping must be in the same column. To demonstrate:
请注意,块映射中键的第一个字符必须在同一列中。展示:
# OK
foo: 1
bar: 2
# Parse error
foo: 1
bar: 2
Sequences
序列
The equivalent of a JSON array in YAML is a sequence, which looks like either of these (which are equivalent):
YAML 中 JSON 数组的等价物是一个序列,它看起来像以下任一(等价的):
# flow style
[ foo bar, baz ]
# block style
- foo bar
- baz
In a block sequence the -s must be in the same column.
在块序列中,-s 必须在同一列中。
JSON to YAML
JSON 到 YAML
Let's turn your JSON into YAML. Here's your JSON:
让我们将您的 JSON 转换为 YAML。这是您的 JSON:
{"AAPL": [
{
"shares": -75.088,
"date": "11/27/2015"
},
{
"shares": 75.088,
"date": "11/26/2015"
},
]}
As a point of trivia, YAML is a superset of JSON, so the above is already valid YAML—but let's actually use YAML's features to make this prettier.
作为一个小知识点,YAML 是 JSON 的超集,所以上面已经是有效的 YAML——但让我们实际使用 YAML 的特性来使它更漂亮。
Starting from the inside out, we have objects that look like this:
从内到外,我们有如下所示的对象:
{
"shares": -75.088,
"date": "11/27/2015"
}
The equivalent YAML mapping is:
等效的 YAML 映射是:
shares: -75.088
date: 11/27/2015
We have two of these in an array (sequence):
我们在一个数组(序列)中有两个:
- shares: -75.088
date: 11/27/2015
- shares: 75.088
date: 11/26/2015
Note how the -s line up and the first characters of the mapping keys line up.
请注意-s如何排列和映射键的第一个字符排列。
Finally, this sequence is itself a value in a mapping with the key AAPL:
最后,这个序列本身就是一个带有键的映射中的值AAPL:
AAPL:
- shares: -75.088
date: 11/27/2015
- shares: 75.088
date: 11/26/2015
Parsing this and converting it back to JSON yields the expected result:
解析它并将其转换回 JSON 会产生预期的结果:
{
"AAPL": [
{
"date": "11/27/2015",
"shares": -75.088
},
{
"date": "11/26/2015",
"shares": 75.088
}
]
}
You can see it (and edit it interactively) here.
您可以在此处查看(并以交互方式对其进行编辑)。
回答by Jon Scobie
Great answer above. Another way is to use the great yaml jq wrapper tool, yq at https://github.com/kislyuk/yq
上面的答案很好。另一种方法是使用伟大的 yaml jq 包装工具,yq 在https://github.com/kislyuk/yq
Save your JSON example to a file, say ex.json and then
将您的 JSON 示例保存到一个文件中,例如 ex.json 然后
yq -y '.' ex.json
AAPL:
- shares: -75.088
date: 11/27/2015
- shares: 75.088
date: 11/26/2015

