YAML等效于JSON中的对象数组 [英] YAML equivalent of array of objects in JSON

查看:674
本文介绍了YAML等效于JSON中的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要转换为YAML的对象的JSON数组.

I have a JSON array of objects that I'm trying to convert to YAML.

{"AAPL": [
  {
    "shares": -75.088,
    "date": "11/27/2015"
  },
  {
    "shares": 75.088,
    "date": "11/26/2015"
  },
]}

YAML中是否存在不只是JSON的等效表示形式?我想做类似的事情

Is there an equivalent representation in YAML that isn't just JSON? I'd like to do something like

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
  }

推荐答案

TL; DR

您想要这个:

TL;DR

You want this:

AAPL:
  - shares: -75.088
    date: 11/27/2015
  - shares: 75.088
    date: 11/26/2015

映射

相当于JSON对象的YAML是一个映射,如下所示:

Mappings

The YAML equivalent of a JSON object is a mapping, which looks like these:

# 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

序列

YAML中的JSON数组的等效项是一个序列,看起来像以下任何一个(等效项):

Sequences

The equivalent of a JSON array in YAML is a sequence, which looks like either of these (which are equivalent):

# flow style
[ foo bar, baz ]

# block style
- foo bar
- baz

在块序列中,-必须位于同一列中.

In a block sequence the -s must be in the same column.

让我们将您的JSON转换为YAML.这是您的JSON:

Let's turn your JSON into YAML. Here's your JSON:

{"AAPL": [
  {
    "shares": -75.088,
    "date": "11/27/2015"
  },
  {
    "shares": 75.088,
    "date": "11/26/2015"
  },
]}

琐碎的一点是,YAML是JSON的超集,因此以上内容已经是有效的YAML,但实际上,让我们使用YAML的功能使它更漂亮.

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.

从内而外开始,我们有一些看起来像这样的对象:

Starting from the inside out, we have objects that look like this:

{
  "shares": -75.088,
  "date": "11/27/2015"
}

等效的YAML映射为:

The equivalent YAML mapping is:

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.

最后,此序列本身就是键AAPL映射中的值:

Finally, this sequence is itself a value in a mapping with the key AAPL:

AAPL:
  - shares: -75.088
    date: 11/27/2015
  - shares: 75.088
    date: 11/26/2015

对此进行解析并将其转换回JSON会产生预期的结果:

Parsing this and converting it back to JSON yields the expected result:

{
  "AAPL": [
    {
      "date": "11/27/2015", 
      "shares": -75.088
    }, 
    {
      "date": "11/26/2015", 
      "shares": 75.088
    }
  ]
}

您可以查看(并进行交互式编辑)

You can see it (and edit it interactively) here.

这篇关于YAML等效于JSON中的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆