跳转至

各种通用格式

JSON

JavaScript Object Notation,其数据类型和JS几乎一致,实际就是JS的一个子集

在JSON出现之前,数据交换一直使用XML,虽然它本身并不复杂,但它有一大堆规范,让人头大。

但JSON出现后,很快发展为ECMA标准的数据交换格式,所有的编程语言都有解析JSON的库。

Json规定了其键以及字符串都必须用双引号括起来,字符集必须是UTF-8。

  • 数组/列表
{
  "list1": ["v1", "v2", "v3"],
  "list2": ["va", "vb", "vc"]
}
  • 对象
{
  "dict1": {"k1": "v1", "k2": "v2", "k3": "v3"},
  "dict2": {
    "ka": "va",
    "kb": "vb",
    "kc": "vc"
  }
}
  • 嵌套
{
  "list_dict": [
    {
      "ka1": "va1",
      "ka2": "va2",
      "ka3": "va3"
    },
    {
      "kb1": "vb1",
      "kb2": "vb2",
      "kb3": "vb3"
    },
    {
      "kc1": "vc1",
      "kc2": "vc2",
      "kc3": "vc3"
    }
  ]
}
  • 纯量
{
  "str": "hello",
  "integer": 77,
  "octal_number": 8,
  "hexadecimal": 15,
  "float": 3.14,
  "exponential": 1.2e+32,
  "bool": [true, false, null],
  "date_simple": "2021-08-14",
  "date_iso": "2016-08-14 13:46:07 -0500"
}

YAML

YAML Ain't a Markup Language

参考文档:https://www.runoob.com/w3cnote/yaml-intro.html

  • 数组/列表
list1:
  - [v1, v2, v3]  # 推荐
list2:
  - va
  - vb
  - vc
  • 对象
dict1: {k1: v1, k2: v2, k3: v3}  # key不需要引号,value都可,建议用
dict2:
  ka: va  # 冒号后必须带空格?
  kb: vb
  kc: vc

"""
{
  'dict1': {
    'ka': 'va',
    'kb': 'vb',
    'kc': 'vc',
    },
  'dict2': {
    'ka': 'va',
    'kb': 'vb',
    'kc': 'vc',
    }
}
"""
  • 嵌套
list_dict:
  - {ka1: "va1", ka2: "va2", ka3: "va3"}
  - {kb1: "vb1", kb2: "vb2", kb3: "vb3"}
  - {kc1: "vc1", kc2: "vc2", kc3: "vc3"}
  • 纯量
# 字符串,带空格或特殊字符的字符串,建议用双引号
str: hello world
# 多行,每句末尾自动加换行(如果想加两个用:|+,不想加用:|-)
multiline: |
  one line
  two line
  three line

integer: 77
float: 3.14
octal_number: 010  # 八进制数:8
hexadecimal: 0xF  # 十六进制数:15
exponential: 2.0e+3  # 2x10^3=2000

bool: [true, false, ~]

# 时间使用ISO-8601格式
date_simple: 2021-08-14
# 时间和日期之间使用T连接,最后使用+代表时区
date_iso: 2021-08-14T15:02:31+08:00

TOML

语法参考:https://toml.io/cn/

  • 数组/列表
list1 = ["v1", "v2", "v3"]
list2 = ["va", "vb", "vc"]
  • 对象
[dict1]
k1 = "v1"
k2 = "v2"
k3 = "v3"
dict2 = {ka="va", kb="vb", kc="vc"}
  • 嵌套
[[list_dict]]
ka1 = "va1"
ka2 = "va2"
ka3 = "va3"

[[list_dict]]
kb1 = "vb1"
kb2 = "vb2"
kb3 = "vb3"

[[list_dict]]
kc1 = "vc1"
kc2 = "vc2"
kc3 = "vc3"
  • 纯量
# 字符串
str = "hello world"

int = 77
float = 3.14
exponential = 2.0e+3  # 2x10^3
bool1 = [true, false]

date = 1979-05-27

INI

*.ini初始化文件(Initialization file),后缀名也可以是.cfg.conf.txt

[section]
; 注释
name=value

最后更新: 2022-03-07