Home > Web Front-end > JS Tutorial > Understanding JSON: 3-Minute Lesson_json

Understanding JSON: 3-Minute Lesson_json

WBOY
Release: 2016-05-16 18:00:14
Original
1161 people have browsed it
  1. 两个月前你从没听说过JSON
  2. 一个月前你听说了这个词但没有留意
  3. 一周前你发现这个词被提到多次,开始想,没错 … 又有一些垃圾东西要学了
  4. 今天你被心灵深处的一个闹铃闹醒,心想:这该死的json究竟是个什么东西?为什么突然间到处都是它了!

于是晚上我乘坐了一辆慢腾腾的公交回到家(周五通常都是很慢),然后给自己找了一大堆关于JSON资料。所以我可以文雅的带你进入JSON的大门。

这就开始了 …

这几个字母是什么意思?

JavaScript Object Notation.

[一个滑稽的名字。它应该被称作Lightweight Ecmascript Object Notation, 或简称 'LEON'。 ;-) ]

它是个什么东西?

JSON是一种传递对象的语法,对象可以是name/value对,数组和其他对象。

下面是一小段JSON代码:

{"skillz": {
	"web":[
		{"name": "html",
		 "years": "5"
		},
		{"name": "css",
		 "years": "3"
		}],
	"database":[
		{"name": "sql",
		 "years": "7"
		}]
}}
Copy after login

你看懂了吧?那么当你再看到它时就知道它是JSON了。主要部分:

花括弧,方括弧,冒号和逗号

  1. 花括弧表示一个“容器”
  2. 方括号装载数组
  3. 名称和值用冒号隔开
  4. 数组元素通过逗号隔开

把它想成“得了厌食症的XML”

(如果你跟我一样老,可以把它想成有层次关系的'.INI'文件)

(如果你是个自以为是的Lisp小丑,可以把它想成”S-expressions”,自以为是吧)

JSON很像XML,因为:

  1. 他们都“自我描述”,这意味着值都是可列举的,是“人类可读”的
  2. 都是有层级的。(例如你可以在值里再存放值)
  3. 都能被多种的编程语言解析和使用
  4. 都能使用AJAX方法来传递(例如httpWebRequest)

JSON跟XML不一样,因为:

  1. XML里在元素的开始和结尾处有尖括号和标签名:JSON使用花括号,而且只在数据的开始和结束时使用。
  2. JSON更简练,毫无疑问更适合人类书写,也许也能让我们更快速的阅读。
  3. JSON可以在JavaScript里简单的传递到eval()方法里使用
  4. JSON里有数组{每个元素没有自己的名称}
  5. 在XML里你可以对一个元素使用任意想要的名称,在JSON里你不能使用Javascript里的保留字

可是为什么?它有什么好的?

当你写ajax之类的东西时,如果你使用JSON,你就勉去了手工拼写XML。更迅速。

同样,当你写ajax之类的东西时,怎样最简单?XML方式还是JSON方式:

XML方式:

  1. 取回一个XML文件
  2. 循环它,从中提取值
  3. 处理这些值,等

对比

JSON方式:

  1. 取回JSON字符串。
  2. ‘eval' JSON数据

Is it object-oriented?

No, strictly speaking, no.

It’s like object-oriented in VB6. It provides a good encapsulation mechanism, you can use it to separate data and methods, but it does not provide any inheritance, polytype, interface, or other similar object-oriented things

Obviously, it is a step forward in making JavaScript easier to maintain, analyze and reuse.

Thomas Frank wrote a flexible javascript library called classyJSON, which adds features such as inheritance and definition scope to JSON code.

Is it only used on the client side?

Yes and no. On the server side you can easily serialize objects to JSON and vice versa. For .net, programmers can use a class library like Json.net to automate these operations (I guess using reflection mechanism), or you can use your own program to do these things, which may be faster some.

3 minutes are almost over….

As far as I know, JSON was invented by a guy named Douglas Crockford. If you like it, you can take a look at his website, he is very interesting.

That’s all.

That’s all I’ve got a few minutes to sort out – all I’ve said is that some of the stuff I’ve said may be completely wrong. If so, please leave me a comment and tell me how stupid I am. I will happily correct any mistakes. Good luck!

(Side note: If you replace { and } with "<" and "/>" and ":" with "/"... you'll get something very much like gaXml Stuff. Interesting world

(Side Note 2: Jason and Ajax are both heroes from Greek mythology. Preview: Other upcoming tech junk includes: Heracles, Perseus, Deucalion, Theseus
and Bellerophon )

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template