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

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

问题描述

我有一个 JSON 对象数组,我正在尝试将其转换为 YAML.

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

你想要这个:

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

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

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天全站免登陆