JSON规范说明和完整语言描述:理想的轻量级数据交换语言

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人类阅读和编写,也易于机器解析和生成。它是基于JavaScript Programming LanguageStandard ECMA-262 3rd Edition - December 1999的一个子集。 JSON是一种完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯,包括C、C++、C#、Java、JavaScript、Perl、Python等。这些特性使JSON成为理想的数据交换语言。

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

简介

JSON建构于两种结构:JSON is built on two structures:

  • “名称/值”集合。不同的语言中,它被理解为对象、纪录、结构、字典、哈希表、键表或者关联数组。
    A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • 值列表。在大部分语言中,它被理解为数组、矢量、列表和序列。
    An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

这些都是通用数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangable with programming languages also be based on these structures.

JSON具有以下这些形式:In JSON, they take on these forms:

对象(object)

对象(object)是一个无序的“名称/值”集合。一个对象以{(左括号)开始、}(右括号)结束。每个名称后跟一个:(冒号);不同的名称/值之间使用,(逗号)分隔。

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

数组(array)

数组是值(value)的有序集合。一个数组以[(左中括号)开始,](右中括号)结束。值之间使用,(逗号)分隔。

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

值(value)

值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象或者数组。这些结构可以嵌套。

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

字符串(string)

字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。字符(character)用单字符串(single character string)表示。字符串与C或者Java的字符串非常相似。

A string is a collection of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

数值(number)

数值(number)也与C或者Java的数值非常相似,除了八进制与十六进制格式不被使用。

A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

空白符(whitespace)

空白符(whitespace)可以加入到任何符号之间。

Whitespace can be inserted between any pair of tokens.

除去一些编码细节,以上就是完整的语言描述。Excepting a few encoding details, that completely describes the language.

完整语法定义

object ::= {} | {members} 
members ::= pair | pair, members
pair ::= string: value
array ::= [] | [elements]
elements ::= value | value, elements
value ::= string | number | object | array | true | false | null
string ::= "" | "chars"
chars ::= char | char chars
char ::= any-Unicode-character-except-"-or-\-or-control-character
| \" | \\ | \/ | \b | \f | \n | \r | \t | \u four-hex-digits
number ::= int | int frac | int exp | int frac exp
int ::= digit | digit1-9 digits | -digit | -digit1-9 digits
frac ::= .digits
exp ::= e digits
digits ::= digit | digit digits
e ::= e | e+ | e- | E | E+ | E-

(以上文章译自JSON官方网站,虽然该网站上也有中文翻译,但是短短一篇文章,错漏近10处,所以全文重译。)

作者: 葱游饼 [zenYoubin]

波波坡原创文章 链接:http://www.bobopo.com/article/code/json.htm

标签: ,

关键词: JSON, Javascript Object Notation, 规范, 语言描述, 数据交换, 格式, C语言, 名称/值, 对象, object, 数组, array, 值, value, 字符串, string, 数值, number, 空白符, whitespace, 官方网站

创建日期: 2009-03-11

修改日期: 2009-03-11

文库 微博 博客 作品 首页