---
title: "什么是JSON"
author: "thirtysix"
date: "2007-09-29T17:27:00Z"
tags: []
slug: "json"
draft: false
lastmod: "2007-09-29T17:27:00Z"
id: 202075
url: "https://thirtysix.iteye.com/blog/202075"
---

### **摘要**

> ```
> JavaScript Object Notation (JSON) 是一种轻量级、基于文本、语言无关的数据交换格式。它是从ECMAScript语言标准衍生而来的。JSON为轻便的表示结构化数据，定义了一小套格式化规则
> ```

### **1.  说明**

> ```
> JSON是结构化数据串行化的文本格式。它衍生自Javascript的对象实字，这定义在ECMAScript语言标准第三版中。
> ```
> 
> ```
> JSON能够描述四种简单的类型（字符串、数字、布尔值及null）和两种结构化类型（对象及数组）。
> ```
> 
> ```
> 字符串(string)是零个或多个Unicode字符的序列。
> ```
> 
> ```
> 对象(object)是无次序的零个或多个名/值(name/value)对的集合，这里的name是string类型，value则可以是string、number、boolean、null、object或array类型。
> ```
> 
> ```
> 数组(array)是零个或多个value的有序序列。
> ```
> 
> ```
> "object"和"array"这两个术语来自JavaScript规范。
> ```
> 
> ```
> JSON的设计目标是使它成为小的、轻便的、文本的，而且是JavaScript的一个子集。  
> ```

```
1.1.  本文档使用的术语/约定
```

> ```
> 本文档中的关键字："MUST"、"MUST NOT"、"REQUIRED"、"SHALL"、"SHALL NOT", "SHOULD"、"SHOULD NOT"、"RECOMMENDED"、"MAY"和"OPTIONAL"，按照[RFC2119]中所描述的来解释。
> ```
> 
> ```
> 本文档中的语法规则按照[RFC4234]中所描述的来解释。 
> ```

### **2.  JSON语法**

> ```
> JSON文本是一个标记符的序列。这套标记符包含六个构造字符、字符串、数字和三个实字名。
> ```
> 
> ```
> JSON文本一个串行化的对象或数组。
> ```
> 
> - JSON-text = object / array
> 
> ```
> 以下是六个构造字符：
> ```
> 
> - begin-array     = ws %x5B ws  ; [ 左方括号
> - begin-object    = ws %x7B ws  ; { 左大括号
> - end-array       = ws %x5D ws  ; ] 右方括号
> - end-object      = ws %x7D ws  ; } 右大括号
> - name-separator  = ws %x3A ws  ; : 冒号
> - value-separator = ws %x2C ws  ; , 逗号
> 
> 在这六个构造字符的前或后允许存在无意义的空白符。
> 
> - ws = \*(
>   
>   ```
>               %x20 /              ; 空格符
>   ```
>   
>   ```
>               %x09 /              ; 水平制表符
>   ```
>   
>   ```
>               %x0A /              ; 换行符
>   ```
>   
>   ```
>               %x0D                ; 回车符
>   ```
>   
>   ```
>        )
>   ```

```
2.1.  值
```

> ```
> JSON必须（MUST）是一个对象、数组、数字或字符串，或者是下面三个实字名之一：
> ```
> 
> - false null true
> 
> 实字名必须（MUST）是小写的，且不允许有其它的实字名。
> 
> - value = false / null / true / object / array / number / string
>   
>   ```
>   false = %x66.61.6c.73.65   ; false
>   ```
>   
>   ```
>   null  = %x6e.75.6c.6c      ; null
>   ```
>   
>   ```
>   true  = %x74.72.75.65      ; true
>   ```

```
2.2.  对象
```

> 对象结构被表示为：一对大括号包围着零个或多个name/value对（或者是成员） 。名是string类型的。每个名后面根一个冒号，把名与值分开。逗号则隔开紧跟在值后的另一个名。对象中的这些名应该（SHOULD）是唯一的。
> 
> - object = begin-object \[ member \*( value-separator member ) ] end-object
> - member = string name-separator value

```
2.3.  数组
```

> ```
> 数组结构表示为：方括号包围着零个或多个值（或是元素）。元素间以逗号相隔。
> ```
> 
> - array = begin-array \[ value \*( value-separator value ) ] end-array

```
2.4.  数字
```

> ```
> 数字的表示法与其它多数编程语言相似。一个数字包括一个可能带着负号的整数成分，它后面可能跟着一个小数部分或是指数部分。
> ```
> 
> ```
> 不允许有八进制及十六进制形式。前面带有0也是被禁止的。
> ```
> 
> ```
> 小数部分是一个小数点后跟着一个或多个阿拉伯数字。
> ```
> 
> ```
> 指数部分以大写或小写的E开头，E后面可以跟一个正/负号。接着是一个或多个阿拉伯数字。
> ```
> 
> ```
> 数字值不能表示成阿拉伯数字数字的序列（如Infinity和Nan是不允许的）。
> ```
> 
> - number = \[ minus ] int \[ frac ] \[ exp ]
>   
>   ```
>   decimal-point = %x2E       ; .
>   ```
>   
>   ```
>   digit1-9 = %x31-39         ; 1-9
>   ```
>   
>   ```
>   e = %x65 / %x45            ; e E
>   ```
>   
>   ```
>   exp = e [ minus / plus ] 1*DIGIT
>   ```
>   
>   ```
>   frac = decimal-point 1*DIGIT
>   ```
>   
>   ```
>   int = zero / ( digit1-9 *DIGIT )
>   ```
>   
>   ```
>   minus = %x2D               ; -
>   ```
>   
>   ```
>   plus = %x2B                ; +
>   ```
>   
>   ```
>   zero = %x30                ; 0
>   ```

```
2.5.  字符串
```

> ```
> 字符串的表示法与C语言家族的规范类似。string以引号开始与结束。所有的Unicode字符都可以放置在引号中，除了转义字符：引号、反斜杆\、控制符（U+0000 - U+001F）。
> ```
> 
> ```
> 所有的这些字符都因该避免。如果字符在基本的多语言环境中（U+0000 - U+FFFF），它可以以这样的六字符序列表示：一个反斜杆，后面跟一个字母U，再跟四个字符代号的十六进制编码的阿拉伯数字。A - F这些十六进制字母可以小写。所以像只包含一个反斜杆的字符可以表示成："\u005C"。
> ```
> 
> ```
> 另一种办法，可以用两个转义字符序列来表示一些常用的字符。所以像只包含一个反斜杆字符的字符串可以更简洁的表示成"\\"。
> ```
> 
> ```
> 转义一个在多语言环境中不存在的字符，这个字符可以表示成一个12字符的序列，编码为UTF-16的代用对（UTF-16 surrogate pair）。所以像只包含一个G音谱字符的字符串可以表示为"\uD834\uDD1E"。
> ```
> 
> - string = quotation-mark \*char quotation-mark
>   
>   ```
>             char = unescaped /
>   ```
>   
>   ```
>                   escape (
>   ```
>   
>   ```
>                       %x22 /          ; "    quotation mark  U+0022
>   ```
>   
>   ```
>                       %x5C /          ; \    reverse solidus U+005C
>   ```
>   
>   ```
>                       %x2F /          ; /    solidus         U+002F
>   ```
>   
>   ```
>                       %x62 /          ; b    backspace       U+0008
>   ```
>   
>   ```
>                       %x66 /          ; f    form feed       U+000C
>   ```
>   
>   ```
>                       %x6E /          ; n    line feed       U+000A
>   ```
>   
>   ```
>                       %x72 /          ; r    carriage return U+000D
>   ```
>   
>   ```
>                       %x74 /          ; t    tab             U+0009
>   ```
>   
>   ```
>                       %x75 4HEXDIG )  ; uXXXX                U+XXXX
>   ```
>   
>   ```
>             escape = %x5C              ; \
>   ```
>   
>   ```
>             quotation-mark = %x22      ; "
>   ```
>   
>   ```
>             unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
>   ```

### **3. 编码**

> ```
> JSON文SHALL被编码为Unicode。缺省的编码为UTF-8。
> ```
> 
> ```
> 因为JSON文本的开头两个字符总是ASCII字符[RFC0020]，我们可以通过查看前面4个字符的0部分，来判断8字节的流是UTF-8还是UTF-16(BE or LE)，或者是UTF-32(BE or LE)。
> ```
> 
> - 00 00 00 xx  UTF-32BE
>   
>   ```
>              00 xx 00 xx  UTF-16BE
>   ```
>   
>   ```
>              xx 00 00 00  UTF-32LE
>   ```
>   
>   ```
>              xx 00 xx 00  UTF-16LE
>   ```
>   
>   ```
>              xx xx xx xx  UTF-8
>   ```

### **4. 解析器**

> ```
> JSON解析器将JSON文本转换成其它的表现形式。它必须（MUST）能够接受符合JSON语法的所有文本。解析器也可以（MAY）接受非JSON的形式或一些扩展。
> ```
> 
> ```
> 实现中，可以设置限制它所接受的文本大小，可以限制JSON文本的最大深度，可以设置数的范围，也可以限制字符串中的字符长度。
> ```

### **5. 生成器**

> ```
> JSON生成器生成JSON文本。目标文本必须（MSUT）严格遵守JSON的语法。 
> ```

### **6. IANA Considerations**

> ```
> JSON文本的MIME类型是application/json。
> ```
> 
> ```
> 类型名：application
> ```
> 
> ```
> 图表类型名：JSON
> ```
> 
> ```
> 必需的参数：n/a
> ```
> 
> ```
> 可选的参数：n/a
> ```
> 
> ```
> 编码选择： 8bit if UTF-8; binary if UTF-16 or UTF-32
> ```
> 
> - *JSON may be represented using UTF-8, UTF-16, or UTF-32.  When JSON is written in UTF-8, JSON is 8bit compatible.  When JSON is written in UTF-16 or UTF-32, the binary content-transfer-encoding must be used.*

> ```
> 安全考虑：
> ```
> 
> 1. 脚本语言通常都有安全问题。JSON是JavaScript的子集，但是它是一个撤消了附值与调用的安全子集。
> 2. JSON文本可以安全的传递个JavaScript的eval()函数。如果字符没有被装入JSON标记符中的标记中，这可以通过JavaScript的两个正则表达式，使用test和replace方法，来快速的判定。

> var my\_JSON\_object = !(/\[^,:{}\\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/.test(
> 
>              text.replace(/"(\\\\.|\[^"\\\\])\*"/g, ''))) &&
> 
>          eval('(' + text + ')');

> 互用性考虑： n/a
> 
> 发布的规范： RFC 4627
> 
> 使用该媒体类型的应用程序：
> 
> - *JSON has been used to exchange data between applications written in all of these programming languages: ActionScript, C, C#, ColdFusion, Common Lisp, E, Erlang, Java, JavaScript, Lua, Objective CAML, Perl, PHP, Python, Rebol, Ruby, and Scheme.*

> ```
> 其它额外信息（略） 
> ```

### **7. Security Considerations**

> 参考第六节中的Security Considerations。

### **8. 例子**

> ```
> 一个JSON对象：
> ```
> 
> - {
>   
>   ```
>         "Image": {
>   ```
>   
>   ```
>             "Width":  800,
>   ```
>   
>   ```
>             "Height": 600,
>   ```
>   
>   ```
>             "Title":  "View from 15th Floor",
>   ```
>   
>   ```
>             "Thumbnail": {
>   ```
>   
>   ```
>                 "Url":    "http://www.example.com/image/481989943",
>   ```
>   
>   ```
>                 "Height": 125,
>   ```
>   
>   ```
>                 "Width":  "100"
>   ```
>   
>   ```
>             },
>   ```
>   
>   ```
>             "IDs": [116, 943, 234, 38793] 
>   ```
>   
>   ```
>           }
>   ```
>   
>   ```
>      }
>   ```
> 
> ```
> 该对象的Image成员是一个对象，而Image的Thumbnail成员也是一个对象，且Image的IDs成员是一个数组。
> ```
> 
> ```
> 下面是一个包含两个对象JSON数组：
> ```
> 
> - [
>   
>   ```
>         {
>   ```
>   
>   ```
>            "precision": "zip",
>   ```
>   
>   ```
>            "Latitude":  37.7668,
>   ```
>   
>   ```
>            "Longitude": -122.3959,
>   ```
>   
>   ```
>            "Address":   "",
>   ```
>   
>   ```
>            "City":      "SAN FRANCISCO",
>   ```
>   
>   ```
>            "State":     "CA",
>   ```
>   
>   ```
>            "Zip":       "94107",
>   ```
>   
>   ```
>            "Country":   "US"
>   ```
>   
>   ```
>         },
>   ```
>   
>   ```
>         {
>   ```
>   
>   ```
>            "precision": "zip",
>   ```
>   
>   ```
>            "Latitude":  37.371991,
>   ```
>   
>   ```
>            "Longitude": -122.026020,
>   ```
>   
>   ```
>            "Address":   "",
>   ```
>   
>   ```
>            "City":      "SUNNYVALE",
>   ```
>   
>   ```
>            "State":     "CA",
>   ```
>   
>   ```
>            "Zip":       "94085",
>   ```
>   
>   ```
>            "Country":   "US"
>   ```
>   
>   ```
>         }
>   ```
>   
>   ```
>      ]
>   ```

### **9. 参考文献**

```
9.1.  标准化参考 
```

```
   [ECMA]    European Computer Manufacturers Association, "ECMAScript
```

```
             Language Specification 3rd Edition", December 1999,
```

```
             
```

```
             ecma-st/ECMA-262.pdf>.
```

```
   [RFC0020] Cerf, V., "ASCII format for network interchange", RFC 20,
```

```
             October 1969.
```

```
   [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
```

```
             Requirement Levels", BCP 14, RFC 2119, March 1997.
```

```
   [RFC4234] Crocker, D. and P.  Overell, "Augmented BNF for Syntax
```

```
             Specifications: ABNF", RFC 4234, October 2005. 
```

```
   [UNICODE] The Unicode Consortium, "The Unicode Standard Version 4.0",
```

```
             2003, http://www.unicode.org/versions/Unicode4.1.0/.
```

```
作者联系方式 
```

```
   Douglas Crockford
```

```
   JSON.org
```

```
   EMail: douglas@crockford.com
```

```
完整的版权声明 
```

```
   Copyright (C) The Internet Society (2006).
```

```
   This document is subject to the rights, licenses and restrictions
```

```
   contained in BCP 78, and except as set forth therein, the authors
```

```
   retain all their rights.
```

```
   This document and the information contained herein are provided on an
```

```
   "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS
```

```
   OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND THE INTERNET
```

```
   ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED,
```

```
   INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE
```

```
   INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
```

```
   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 
```

```
知识产权 
```

```
   The IETF takes no position regarding the validity or scope of any
```

```
   Intellectual Property Rights or other rights that might be claimed to
```

```
   pertain to the implementation or use of the technology described in
```

```
   this document or the extent to which any license under such rights
```

```
   might or might not be available; nor does it represent that it has
```

```
   made any independent effort to identify any such rights.  Information
```

```
   on the procedures with respect to rights in RFC documents can be
```

```
   found in BCP 78 and BCP 79.
```

```
   Copies of IPR disclosures made to the IETF Secretariat and any
```

```
   assurances of licenses to be made available, or the result of an
```

```
   attempt made to obtain a general license or permission for the use of
```

```
   such proprietary rights by implementers or users of this
```

```
   specification can be obtained from the IETF on-line IPR repository at
```

```
   http://www.ietf.org/ipr.
```

```
   The IETF invites any interested party to bring to its attention any
```

```
   copyrights, patents or patent applications, or other proprietary
```

```
   rights that may cover technology that may be required to implement
```

```
   this standard.  Please address the information to the IETF at
```

```
   ietf-ipr@ietf.org. 
```

```
鸣谢 
```

```
   Funding for the RFC Editor function is provided by the IETF
```

```
   Administrative Support Activity (IASA).
```