Network Working Group D. Crockford Request for Comments: 4627 JSON.org Category: Informational July 2006
Network Working Group D. Crockford Request for Comments: 4627 JSON.org Category: Informational July 2006
The application/json Media Type for JavaScript Object Notation (JSON)
JavaScript对象表示法(json)的应用程序/json媒体类型
Status of This Memo
关于下段备忘
This memo provides information for the Internet community. It does not specify an Internet standard of any kind. Distribution of this memo is unlimited.
本备忘录为互联网社区提供信息。它没有规定任何类型的互联网标准。本备忘录的分发不受限制。
Copyright Notice
版权公告
Copyright (C) The Internet Society (2006).
版权所有(C)互联网协会(2006年)。
Abstract
摘要
JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format. It was derived from the ECMAScript Programming Language Standard. JSON defines a small set of formatting rules for the portable representation of structured data.
JavaScript对象表示法(JSON)是一种轻量级、基于文本、独立于语言的数据交换格式。它源于ECMAScript编程语言标准。JSON为结构化数据的可移植表示定义了一小组格式化规则。
JavaScript Object Notation (JSON) is a text format for the serialization of structured data. It is derived from the object literals of JavaScript, as defined in the ECMAScript Programming Language Standard, Third Edition [ECMA].
JavaScript对象表示法(JSON)是用于结构化数据序列化的文本格式。它源自JavaScript的对象文本,如ECMAScript编程语言标准第三版[ECMA]中所定义。
JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).
JSON可以表示四种基本类型(字符串、数字、布尔值和null)和两种结构化类型(对象和数组)。
A string is a sequence of zero or more Unicode characters [UNICODE].
字符串是零个或多个Unicode字符[Unicode]的序列。
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
对象是零个或多个名称/值对的无序集合,其中名称是字符串,值是字符串、数字、布尔值、null、对象或数组。
An array is an ordered sequence of zero or more values.
数组是零个或多个值的有序序列。
The terms "object" and "array" come from the conventions of JavaScript.
术语“对象”和“数组”来自JavaScript的约定。
JSON's design goals were for it to be minimal, portable, textual, and a subset of JavaScript.
JSON的设计目标是使其最小化、可移植、文本化,并且是JavaScript的一个子集。
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119].
本文件中的关键词“必须”、“不得”、“必需”、“应”、“不应”、“应”、“不应”、“建议”、“可”和“可选”应按照[RFC2119]中所述进行解释。
The grammatical rules in this document are to be interpreted as described in [RFC4234].
本文件中的语法规则应按照[RFC4234]中所述进行解释。
A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.
JSON文本是一系列标记。标记集包括六个结构字符、字符串、数字和三个文字名称。
A JSON text is a serialized object or array.
JSON文本是序列化的对象或数组。
JSON-text = object / array
JSON-text = object / array
These are the six structural characters:
以下是六个结构特征:
begin-array = ws %x5B ws ; [ left square bracket
开始数组=ws%x5B ws;[左方括号
begin-object = ws %x7B ws ; { left curly bracket
begin-object = ws %x7B ws ; { left curly bracket
end-array = ws %x5D ws ; ] right square bracket
结束数组=ws%x5D ws;]右方括号
end-object = ws %x7D ws ; } right curly bracket
end-object = ws %x7D ws ; } right curly bracket
name-separator = ws %x3A ws ; : colon
name-separator = ws %x3A ws ; : colon
value-separator = ws %x2C ws ; , comma
值分隔符=ws%x2C ws,逗号
Insignificant whitespace is allowed before or after any of the six structural characters.
允许在六个结构字符中的任何一个之前或之后使用不重要的空格。
ws = *( %x20 / ; Space %x09 / ; Horizontal tab %x0A / ; Line feed or New line %x0D ; Carriage return )
ws = *( %x20 / ; Space %x09 / ; Horizontal tab %x0A / ; Line feed or New line %x0D ; Carriage return )
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
JSON值必须是对象、数组、数字或字符串,或以下三个文字名称之一:
false null true
假空真
The literal names MUST be lowercase. No other literal names are allowed.
文字名称必须为小写。不允许使用其他文字名称。
value = false / null / true / object / array / number / string
value = false / null / true / object / array / number / string
false = %x66.61.6c.73.65 ; false
false = %x66.61.6c.73.65 ; false
null = %x6e.75.6c.6c ; null
null = %x6e.75.6c.6c ; null
true = %x74.72.75.65 ; true
true = %x74.72.75.65 ; true
An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.
对象结构表示为围绕零个或多个名称/值对(或成员)的一对花括号。名称是一个字符串。每个名称后面都有一个冒号,将名称与值分开。一个逗号将值与以下名称分隔开。对象中的名称应该是唯一的。
object = begin-object [ member *( value-separator member ) ] end-object
对象=开始对象[成员*(值分隔符成员)]结束对象
member = string name-separator value
member = string name-separator value
An array structure is represented as square brackets surrounding zero or more values (or elements). Elements are separated by commas.
数组结构表示为围绕零个或多个值(或元素)的方括号。元素之间用逗号分隔。
array = begin-array [ value *( value-separator value ) ] end-array
数组=开始数组[值*(值分隔符值)]结束数组
The representation of numbers is similar to that used in most programming languages. A number contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part.
数字的表示法与大多数编程语言中使用的表示法相似。数字包含一个整数成分,该成分的前缀可能是可选的减号,后面可能是分数部分和/或指数部分。
Octal and hex forms are not allowed. Leading zeros are not allowed.
不允许八进制和十六进制形式。不允许使用前导零。
A fraction part is a decimal point followed by one or more digits.
小数部分是一个小数点,后跟一个或多个数字。
An exponent part begins with the letter E in upper or lowercase, which may be followed by a plus or minus sign. The E and optional sign are followed by one or more digits.
指数部分以大写或小写字母E开头,后面可以跟加号或减号。E和可选符号后跟一个或多个数字。
Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted.
不允许使用不能表示为数字序列的数值(如无穷大和NaN)。
number = [ minus ] int [ frac ] [ exp ]
number = [ minus ] int [ frac ] [ exp ]
decimal-point = %x2E ; .
decimal-point = %x2E ; .
digit1-9 = %x31-39 ; 1-9
digit1-9 = %x31-39 ; 1-9
e = %x65 / %x45 ; e E
e = %x65 / %x45 ; e E
exp = e [ minus / plus ] 1*DIGIT
exp = e [ minus / plus ] 1*DIGIT
frac = decimal-point 1*DIGIT
分数=小数点1*位
int = zero / ( digit1-9 *DIGIT )
int = zero / ( digit1-9 *DIGIT )
minus = %x2D ; -
minus = %x2D ; -
plus = %x2B ; +
plus = %x2B ; +
zero = %x30 ; 0
zero = %x30 ; 0
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).
字符串的表示类似于C语言家族中使用的约定。字符串以引号开头和结尾。除必须转义的字符外,所有Unicode字符都可以放在引号内:引号、反索利多卡因和控制字符(U+0000到U+001F)。
Any character may be escaped. If the character is in the Basic Multilingual Plane (U+0000 through U+FFFF), then it may be represented as a six-character sequence: a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point. The hexadecimal letters A though F can be upper or lowercase. So, for example, a string containing only a single reverse solidus character may be represented as "\u005C".
任何字符都可以转义。如果字符位于基本多语言平面(U+0000到U+FFFF),则可以将其表示为六个字符的序列:一个反向索利多卡因,后跟小写字母U,后跟四个十六进制数字,用于编码字符的代码点。十六进制字母A到F可以是大写或小写。因此,例如,仅包含单个反向索利多士字符的字符串可以表示为“\u005C”。
Alternatively, there are two-character sequence escape representations of some popular characters. So, for example, a string containing only a single reverse solidus character may be represented more compactly as "\\".
或者,有些流行字符有两个字符序列转义表示。因此,例如,仅包含单个反向索利多士字符的字符串可以更简洁地表示为“\\”。
To escape an extended character that is not in the Basic Multilingual Plane, the character is represented as a twelve-character sequence, encoding the UTF-16 surrogate pair. So, for example, a string containing only the G clef character (U+1D11E) may be represented as "\uD834\uDD1E".
要转义不在基本多语言平面中的扩展字符,该字符将表示为12个字符的序列,对UTF-16代理项对进行编码。因此,例如,仅包含G谱号字符(U+1D11E)的字符串可以表示为“\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
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 ; \
escape = %x5C ; \
quotation-mark = %x22 ; "
quotation-mark = %x22 ; "
unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
JSON文本应采用Unicode编码。默认编码是UTF-8。
Since the first two characters of a JSON text will always be ASCII characters [RFC0020], it is possible to determine whether an octet stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking at the pattern of nulls in the first four octets.
由于JSON文本的前两个字符始终是ASCII字符[RFC0020],因此可以通过查看前四个八位字节中的空模式来确定八位字节流是UTF-8、UTF-16(be或LE)还是UTF-32(be或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
00 00 xx UTF-32BE 00 xx 00 xx UTF-16BE xx 00 00 UTF-32LE xx 00 xx 00 UTF-16LE xx xx xx xx xx UTF-8
A JSON parser transforms a JSON text into another representation. A JSON parser MUST accept all texts that conform to the JSON grammar. A JSON parser MAY accept non-JSON forms or extensions.
JSON解析器将JSON文本转换为另一种表示形式。JSON解析器必须接受符合JSON语法的所有文本。JSON解析器可以接受非JSON形式或扩展。
An implementation may set limits on the size of texts that it accepts. An implementation may set limits on the maximum depth of nesting. An implementation may set limits on the range of numbers. An implementation may set limits on the length and character contents of strings.
一个实现可以对它接受的文本的大小设置限制。实现可能会对嵌套的最大深度设置限制。实现可能会对数字的范围设置限制。实现可以对字符串的长度和字符内容设置限制。
A JSON generator produces JSON text. The resulting text MUST strictly conform to the JSON grammar.
JSON生成器生成JSON文本。生成的文本必须严格符合JSON语法。
The MIME media type for JSON text is application/json.
JSON文本的MIME媒体类型为application/JSON。
Type name: application
类型名称:应用程序
Subtype name: json
子类型名称:json
Required parameters: n/a
所需参数:不适用
Optional parameters: n/a
可选参数:不适用
Encoding considerations: 8bit if UTF-8; binary if UTF-16 or UTF-32
编码注意事项:UTF-8时为8位;如果是UTF-16或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.
JSON可以使用UTF-8、UTF-16或UTF-32表示。用UTF-8编写JSON时,JSON是8bit兼容的。当JSON以UTF-16或UTF-32编写时,必须使用二进制内容传输编码。
Security considerations:
安全考虑:
Generally there are security issues with scripting languages. JSON is a subset of JavaScript, but it is a safe subset that excludes assignment and invocation.
脚本语言通常存在安全问题。JSON是JavaScript的子集,但它是排除赋值和调用的安全子集。
A JSON text can be safely passed into JavaScript's eval() function (which compiles and executes a string) if all the characters not enclosed in strings are in the set of characters that form JSON tokens. This can be quickly determined in JavaScript with two regular expressions and calls to the test and replace methods.
如果字符串中未包含的所有字符都在构成JSON标记的字符集中,则JSON文本可以安全地传递到JavaScript的eval()函数(该函数编译并执行字符串)。这可以在JavaScript中通过两个正则表达式和对test和replace方法的调用快速确定。
var my_JSON_object = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( text.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + text + ')');
var my_JSON_object = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( text.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + text + ')');
Interoperability considerations: n/a
互操作性注意事项:不适用
Published specification: RFC 4627
已发布规范:RFC4627
Applications that use this media type:
使用此媒体类型的应用程序:
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.
JSON被用来在所有这些编程语言编写的应用程序之间交换数据:ActionScript、C、C#、ColdFusion、Common Lisp、E、Erlang、Java、JavaScript、Lua、Objective CAML、Perl、PHP、Python、Rebol、Ruby和Scheme。
Additional information:
其他信息:
Magic number(s): n/a File extension(s): .json Macintosh file type code(s): TEXT
Magic number(s): n/a File extension(s): .json Macintosh file type code(s): TEXT
Person & email address to contact for further information: Douglas Crockford douglas@crockford.com
联系人和电子邮件地址,以获取更多信息:Douglas Crockforddouglas@crockford.com
Intended usage: COMMON
预期用途:普通
Restrictions on usage: none
使用限制:无
Author: Douglas Crockford douglas@crockford.com
作者:道格拉斯·克罗克福德douglas@crockford.com
Change controller: Douglas Crockford douglas@crockford.com
变更控制员:道格拉斯·克罗克福德douglas@crockford.com
See Security Considerations in Section 6.
参见第6节中的安全注意事项。
This is a JSON object:
这是一个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": { "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]
} }
} }
Its Image member is an object whose Thumbnail member is an object and whose IDs member is an array of numbers.
它的图像成员是一个对象,其缩略图成员是一个对象,其IDs成员是一个数字数组。
This is a JSON array containing two objects:
这是一个包含两个对象的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" } ]
[ { "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" } ]
[ECMA] European Computer Manufacturers Association, "ECMAScript Language Specification 3rd Edition", December 1999, <http://www.ecma-international.org/publications/files/ ecma-st/ECMA-262.pdf>.
[ECMA]欧洲计算机制造商协会,“ECMAScript语言规范第3版”,1999年12月<http://www.ecma-international.org/publications/files/ ecma st/ecma-262.pdf>。
[RFC0020] Cerf, V., "ASCII format for network interchange", RFC 20, October 1969.
[RFC0020]Cerf,V.,“网络交换的ASCII格式”,RFC 20,1969年10月。
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, March 1997.
[RFC2119]Bradner,S.,“RFC中用于表示需求水平的关键词”,BCP 14,RFC 2119,1997年3月。
[RFC4234] Crocker, D. and P. Overell, "Augmented BNF for Syntax Specifications: ABNF", RFC 4234, October 2005.
[RFC4234]Crocker,D.和P.Overell,“语法规范的扩充BNF:ABNF”,RFC 4234,2005年10月。
[UNICODE] The Unicode Consortium, "The Unicode Standard Version 4.0", 2003, <http://www.unicode.org/versions/Unicode4.1.0/>.
[UNICODE]UNICODE联盟,“UNICODE标准版本4.0”,2003年<http://www.unicode.org/versions/Unicode4.1.0/>.
Author's Address
作者地址
Douglas Crockford JSON.org EMail: douglas@crockford.com
Douglas Crockford JSON.org电子邮件:douglas@crockford.com
Full Copyright Statement
完整版权声明
Copyright (C) The Internet Society (2006).
版权所有(C)互联网协会(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.
本文件受BCP 78中包含的权利、许可和限制的约束,除其中规定外,作者保留其所有权利。
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.
本文件及其包含的信息是按“原样”提供的,贡献者、他/她所代表或赞助的组织(如有)、互联网协会和互联网工程任务组不承担任何明示或暗示的担保,包括但不限于任何保证,即使用本文中的信息不会侵犯任何权利,或对适销性或特定用途适用性的任何默示保证。
Intellectual Property
知识产权
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.
IETF对可能声称与本文件所述技术的实施或使用有关的任何知识产权或其他权利的有效性或范围,或此类权利下的任何许可可能或可能不可用的程度,不采取任何立场;它也不表示它已作出任何独立努力来确定任何此类权利。有关RFC文件中权利的程序信息,请参见BCP 78和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.
向IETF秘书处披露的知识产权副本和任何许可证保证,或本规范实施者或用户试图获得使用此类专有权利的一般许可证或许可的结果,可从IETF在线知识产权存储库获取,网址为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.
IETF邀请任何相关方提请其注意任何版权、专利或专利申请,或其他可能涵盖实施本标准所需技术的专有权利。请将信息发送至IETF的IETF-ipr@ietf.org.
Acknowledgement
确认
Funding for the RFC Editor function is provided by the IETF Administrative Support Activity (IASA).
RFC编辑器功能的资金由IETF行政支持活动(IASA)提供。