语法规则

JSON 语法是 JavaScript 对象表示语法的子集(JavaScript Object Notation, JS 对象简谱)。

  • 数据在名称/值对中
  • 数据由逗号分隔
  • 大括号{}保存对象
  • 中括号[]保存数组

示例一:

  1. {
  2. "employees":[
  3. {
  4. "firstName":"John",
  5. "lastName":"Doe"
  6. },
  7. {
  8. "firstName":"Anna",
  9. "lastName":"Smith"
  10. },
  11. {
  12. "firstName":"Peter",
  13. "lastName":"Jones"
  14. }
  15. ]
  16. }

示例二:

  1. {
  2. "animals": {
  3. "dog": [
  4. {
  5. "name": "Rufus",
  6. "age":15
  7. },
  8. {
  9. "name": "Marty",
  10. "age": null
  11. }
  12. ]
  13. }
  14. }

不支持注释

属于故意设计。

I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability. I know that the lack of comments makes some people sad, but it shouldn’t. Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser.

大神注意到有人利用注释来制定解析规则,这破坏了互操作性(Interoperability),因此大神将其剔除。

其他一些间接添加注释的方法,比如添加:

  1. "_comment": "comment text goes here...",

或使用一些可以删除注释和格式化的接口JSON.minify()来格式化JSON代码,保证格式化后的代码可以运行。
但总体还是不要去使用注释,使用成员名来进行描述,但想一些特别复杂的,也还是有注释的话会更好理解,没有注释可能根本都无法理解~~