JSON(JavaScript Object Notation, /ˈdʒeɪsən/, JavaScript对象表示法)是由美国程序员道格拉斯·克罗克福特构想和设计的一种轻量级资料交换格式。其内容由属性和值所组成,因此也有易于阅读和处理的优势。JSON是独立于编程语言的资料格式,其不仅是JavaScript的子集,也采用了C语言家族的习惯用法,目前也有许多编程语言都能够将其解析和字符串化,其广泛使用的程度也使其成为通用的资料格式。
JSON格式是1999年《JavaScript Programming Language, Standard ECMA-262 3rd Edition》的子集合,所以可以在JavaScript以 eval()
函数(javascript通过eval()调用解析器)读入。不过这并不代表JSON无法使用于其他语言,事实上几乎所有与网络开发相关的语言都有JSON函数库。
JSON的基本数据类型(6种):
- 数值:十进制数,不能有前导0,可以为负数,可以有小数部分。还可以用
e
或者E
表示指数部分。不能包含非数,如NaN。不区分整数与浮点数。JavaScript用双精度浮点数表示所有数值(后来也支持 BigInt[1])。 - 字符串:以双引号
""
括起来的零个或多个Unicode码位。支持反斜杠开始的转义字符序列。 - 布尔值:表示为
true
或者false
。 - 数组:有序的零个或者多个值。每个值可以为任意类型。数组使用方括号
[]
包裹。多个数组元素之间用逗号,
分隔,形如:[value, value]
。 - 对象:若干无序的“键-值对”(key-value pairs),其中键只能是字符串[2]。建议但不强制要求对象中的键是独一无二的。对象以花括号
{}
包裹。多个键-值对之间使用逗号,
分隔。键与值之间用冒号:
分隔。 - 空值:值写为
null
token(6种标点符号、字符串、数值、3种字面量)之间可以存在有限的空白符并被忽略。四个特定字符被认为是空白符:空格符、水平制表符、回车符、换行符。空白符不能出现在token内部(但空格符可以出现在字符串内部)。JSON标准不允许有字节序掩码,不提供注释的句法。 一个有效的JSON文档的根节点必须是一个对象或一个数组。
JSON交换时必须编码为UTF-8。[3]转义序列可以为:“\\”、“\””、“\/”、“\b”、“\f”、“\n”、“\r”、“\t”,或Unicode16进制转义字符序列(\u后面跟随4位16进制数字)。对于不在基本多文种平面上的码位,必须用UTF-16代理对(surrogate pair)表示,例如对于Emoji字符——喜极而泣的表情(U+1F602 😂 FACE WITH TEARS OF JOY)在JSON中应表示为:
1 |
{ "face": "😂" } |
或者:
1 |
{ "face": "\uD83D\uDE02" } |
JSON的格式描述可以参考RFC 4627。
JSON 的痛点
- 痛点一:不能再注释。
通常,可以用 // 或者 # 等作为注释,但是 JSON 中不支持。
- 痛点二:序列化之后的 key 被加上了双引号。
比如:
1 2 3 4 5 6 7 8 9 10 11 12 |
static class Cls { private String name; private Integer age; } public static void main(String[] args) { Cls obj = new Cls(); obj.setAge(100); obj.setName("zhangsan"); System.out.println(JSON.toJSONString(obj)); } |
输出:
1 2 3 4 |
{ "age": 100, "name": "zhangsan" } |
可以发现,将对象转为字符串后,key 都加上了引号 。
JSON5 规范
JSON5 is an extension to the popular JSON file format that aims to be easier to write and maintain by hand (e.g. for config files). It is not intended to be used for machine-to-machine communication. (Keep using JSON or other file formats for that. 🙂)
JSON5 was started in 2012, and as of 2022(截至 2022 年), now gets >65M downloads/week, ranks in the top 0.1% of the most depended-upon packages on npm, and has been adopted by major projects like Chromium, Next.js, Babel, Retool, WebStorm, and more. It’s also natively supported on Apple platforms like MacOS and iOS.
Formally, the JSON5 Data Interchange Format is a superset of JSON (so valid JSON files will always be valid JSON5 files) that expands its syntax to include some productions from ECMAScript 5.1 (ES5). It’s also a subset of ES5, so valid JSON5 files will always be valid ES5.*
JSON5 数据交换格式是 JSON 的超集(因此有效的 JSON 文件始终是有效的 JSON5 文件),它扩展了语法以包含 ECMAScript 5.1(ES5)中的一些产品。
This JavaScript library is a reference implementation for JSON5 parsing and serialization, and is directly used in many of the popular projects mentioned above (where e.g. extreme performance isn’t necessary), but others have created many other libraries across many other platforms.
Summary of Features
The following ECMAScript 5.1 features, which are not supported in JSON, have been extended to JSON5.
Objects
- Object keys may be an ECMAScript 5.1 IdentifierName.【对象的 key 可以跟 JavaScript 中对象 key 完全一致】
- Objects may have a single trailing comma.【末尾可以有一个逗号】
Arrays
- Arrays may have a single trailing comma.
Strings
- Strings may be single quoted.【字符串可以用单引号】
- Strings may span multiple lines by escaping new line characters.【字符串可以用反引号】
- Strings may include character escapes.【字符串可以用转义字符】
Numbers
- Numbers may be hexadecimal.【数字可以是 16 进制】
- Numbers may have a leading or trailing(追踪) decimal point.【数字可以用点开头或结尾】
- Numbers may be IEEE 754 positive infinity, negative infinity, and NaN.【数字可以表示正无穷、负无穷和NaN.】
- Numbers may begin with an explicit plus sign.【数字可以用加号开头】
Comments
- Single and multi-line comments are allowed.【支持单行和多行注释】
White Space
- Additional white space characters are allowed.【允许多余的空格】
Java中怎么使用 JSON5 ?
很遗憾,目前的主流 Java JSON 库大都不支持 JSON5 ,比如阿里巴巴开源的 JSON 库 Fastjson 不支持 JSON5。它只能处理标准的 JSON 格式,无法解析 JSON5 的特性(如注释、单引号字符串、尾随逗号等)。Jackson
是另一个广泛使用的 JSON 库,功能强大且扩展性强。Jackson 本身也不直接支持 JSON5,但可以通过扩展来实现对 JSON5 的支持(具体可以上网查询相关资料)。
相关文章:https://json5.org/