JSON(JavaScript Object Notation),一種輕量級的數據交換格式,源於JavaScript,爲ECMAScript的一個子集。其易於閱讀和編寫、易於編碼和解析等特性,已被廣泛應用於尤其配置及http下的序列化等衆多方面。
json語法不支持註釋,不支持字符串換行,所有的key
都必須雙引號,末尾不能有多餘的逗號...等等,一大堆極其嚴格的要求和不支持的功能。
JSON5 和 JSON 的關係就像 ECMAScript 5 和 ECMAScript 3 的關係。JSON5 同樣是 ECMAScript 5 的嚴格的子集。
JSON5是對JSON的擴展,其目的是能夠讓人們(程序猿)更加容易的閱讀和編寫。
按照官方介紹,JSON5相對於JSON增加了以下特性(優勢) summary-of-features:
這完全就是我想要的!安利給大家。
Json5 語法說明
JSON5
是對JSON
的擴展,讓人可以更容易手工編寫和維護。
JSON5
的特性如下:
對象 Objects
- key值允許沒有雙引號 Object keys may be an ECMAScript 5.1 IdentifierName.
{
code:1,
msg:"Hello"
}
- 允許有多餘的逗號結尾 Objects may have a single trailing comma.
{
code:1,
msg:"Hello",
}
- key值可以使用單引號來包裹 The key value can be wrapped in single quoted.
{
$name:"HellO",
code:1,
msg:'Hello',
}
數組 Arrays
- 允許有多餘的逗號結尾
[1, 2, 3,]
字符串 Strings
- 允許使用單引號包裹字符串 Strings may be single quoted.
{
$name:'HellO',
code:1,
msg:'Hello',
}
- 字符串可以換行,可以多行 Strings may span multiple lines by escaping new line characters
{
$name:'HellO',
$desc:"hello
world!
haha!
",
code:1,
msg:'Hello',
}
- 字符串允許使用轉義字符 Strings may include character escapes.
{
code:1,
msg:"Hello\nWorld\n!",
}
數字 Numbers
- 數字可以用十六進制表示 Numbers may be hexadecimal.
{
code:0xFF
}
- 允許使用小數點開頭或結尾的數字,例如:
.007
,7.
Numbers may have a leading or trailing decimal point.
{
a:.007,
b:7.
f:-.32e-3
}
- 數字可以使用正無窮大、負無窮大、和Nan 來表示, Numbers may be IEEE 754 positive infinity, negative infinity, and NaN.
{
p:Infinity,
n:-Infinity,
b:NaN
}
- 數字前面可以有一個正號
+
Numbers may begin with an explicit plus sign. { a:+10 }
註釋 Comments
- 支持單行註釋和多行註釋 Single and multi-line comments are allowed.
{
// code表示結果代碼
code:1
msg:"返回結果說明"
/*
json5的多行註釋
真是好爽啊,
完全就是爲Panda api而設計的
*/
}
允許多餘的空白符 White Space
- 允許在任何在json5相關符號之前和之後都可能出現多餘的空白符。 Additional white space characters are allowed. 支持的空白符如下:
Code Points Description
U+0009: Horizontal tab
U+000A: Line feed
U+000B: Vertical tab
U+000C: Form feed
U+000D: Carriage return
U+0020: Space
U+00A0: Non-breaking space
U+2028: Line separator
U+2029: Paragraph separator
U+FEFF: Byte order mark
Unicode: Zs category Any other character in the Space Separator Unicode category
官方相關說明介紹:
https://github.com/json5/json5github.com
經典前端面試題每日更新,歡迎參與討論,地址:https://github.com/daily-interview/fe-interview。