JSON 备忘清单

这是理解和编写 JSON 格式配置文件的快速参考备忘单。

入门

介绍

JSON 是一种基于文本的轻量级开放标准,专为人类可读的数据交换而设计。

  • JSON 代表 JavaScript 对象表示法
  • JSON 易于读写。
  • JSON 是与语言无关的数据交换格式
  • JSON 文件扩展名为 .json
  • JSON Internet 媒体类型为 application/json

示例

  1. {
  2. "name": "Jason",
  3. "age": 39,
  4. "height": 1.92,
  5. "gender": "M",
  6. "salary": 70000,
  7. "married": true,
  8. "children": [
  9. {"name": "Tom", "age": 9, "gender":"M"},
  10. {"name": "Ava", "age": 7, "gender":"F"}
  11. ]
  12. }

类型

类型 描述
Number 双精度浮点
String 字符系列
Boolean “true”或“false”
Array 有序的值序列
Value 字符串、数字、布尔值、空值等
Object 键/值对的无序集合
null Null 或 Empty

字符串

\" 双引号 Double quote
\\ 反斜杠 Backslash
\/ 正斜杠 Forward slash
\b 退格 Backspace
\f 换页 Form feed
\n 换行 Newline
\r 回车 Carriage return
\t 标签 Tab
\u 后跟四个十六进制数字

示例

  1. {
  2. "url": "https://quickref.me",
  3. "msg" : "Hi,\n\"QuickRef.ME\"",
  4. "intro": "Share quick reference and cheat sheet for developers."
  5. }

无效字符串

  1. { "foo": 'bar' }

Have to be delimited by double quotes

数字

类型 说明
Integer 数字 1-9、0 和正数或负数
Fraction 0.3、3.9 等分数
Exponent 指数,如 e、e+、e-、E、E+、E

示例

  1. {
  2. "positive" : 12,
  3. "negative" : -1,
  4. "fraction" : 10.25,
  5. "exponent" : 1.0E+2,
  6. "zero" : 0
  7. }

无效的数字

  1. { "foo": 0xFF }

在JSON中,只能使用十进制文字

对象 Objects

  1. {
  2. "color": "Purple",
  3. "id": "210",
  4. "composition": {
  5. "R": 70,
  6. "G": 39,
  7. "B": 89
  8. },
  9. "empty_object": {}
  10. }

用逗号分隔的多个键/值对

数组 Arrays

  1. [1, 2, 3, 4, 5]

[ 开始并以 ] 结束

对象数组

  1. {
  2. "children": [
  3. { "name": "Jimmy Smith", "age": 15 },
  4. { "name": "Sammy Sosa", "age": 12 }
  5. ]
  6. }

数组对象

  1. {
  2. "attributes": ["a1", "a2"],
  3. "methods": ["getter", "setter"],
  4. "empty_array": []
  5. }

二维阵列

  1. {
  2. "my_sequences": [
  3. [1, 2, 3],
  4. [4, 5, 6],
  5. [7, 8, 9, 0],
  6. [10, 11]
  7. ]
  8. }

对象的对象

  1. {
  2. "Mark McGwire": {
  3. "hr": 65,
  4. "avg": 0.278
  5. },
  6. "Sammy Sosa": {
  7. "hr": 63,
  8. "avg": 0.288
  9. }
  10. }

嵌套

  1. {
  2. "Jack": {
  3. "id": 1,
  4. "name": "Franc",
  5. "salary": 25000,
  6. "hobby": ["a", "b"],
  7. "location": {
  8. "country": "A", "city": "A-A"
  9. }
  10. }
  11. }

在 JavaScript 中访问 JSON

访问对象

  1. let myObject = {
  2. "name": "Jason",
  3. "last": "Doe",
  4. "age": 39,
  5. "gender": "M",
  6. "salary": 70000,
  7. "married": true
  8. };

myObject.name “Jason”
myObject["name"] “Jason”
myObject.age 39
myObject.other undefined
myObject[0] undefined

访问嵌套

  1. let myObject = {
  2. "ref": {
  3. "name": 0,
  4. "last": 1,
  5. "age": 2,
  6. "gender": 3,
  7. "salary": 4,
  8. "married": 5
  9. },
  10. "jdoe": [
  11. "Jason",
  12. "Doe",
  13. 39,
  14. "M",
  15. 70000,
  16. true
  17. ],
  18. "jsmith": [
  19. "Tom",
  20. "Smith",
  21. 42,
  22. "F",
  23. 80000,
  24. true
  25. ]
  26. };

myObject.ref.age 2
myObject["ref"]["age"] 2
myObject.jdoe [“Jason”, “Doe”, 39 …]
myObject.jsmith[3] “F”
myObject[1] undefined

访问对象数组

  1. let myArray = [
  2. {
  3. "name": "Jason",
  4. "last": "Doe",
  5. "age": 39,
  6. "gender": "M",
  7. "salary": 70000,
  8. "married": true
  9. },
  10. {
  11. "name": "Tom",
  12. "last": "Smith",
  13. "age": 42,
  14. "gender": "F",
  15. "salary": 80000,
  16. "married": true
  17. },
  18. {
  19. "name": "Amy",
  20. "last": "Burnquist",
  21. "age": 29,
  22. "gender": "F",
  23. "salary": 60000,
  24. "married": false
  25. }
  26. ];

myArray[0] {“name”: “Jason”, …}
myArray[1].name “Tom”
myArray[1][2] 42
myArray[3] undefined
myArray[3].gender TypeError: Cannot read…

访问阵列

  1. let myArray = [
  2. "Jason",
  3. "Doe",
  4. 39,
  5. "M",
  6. 70000,
  7. true
  8. ];

myArray[1] “Doe”
myArray[5] true
myArray[6] undefined

另见