A text interpretation JSON (rpm)

JSON as the current mainstream Web data exchange format, each IT technician must be to understand a data exchange format. Especially in the popular Ajax and REST today's technology, JSON data interchange format will undoubtedly become the first choice !

Today we are together to learn about JSON knowledge of it!
Here Insert Picture Description

A, XML

Speaking before the JSON, I feel the need to take you look at the XML (Extensible Markup Language Extensible Markup Language), because JSON is slowly replaced by XML.

1.XML origin

The amount of the early Web development and data load is not great, so basically rely on HTML (1989 born) can be resolved. But with the growing Web application, some of the shortcomings of HTML are slowly emerging, such as: poor readability, resolved a long time, descriptive data and poor.

February 10, 1998, W3C (World WideⅥiebConsortium, World Wide Web Consortium) announced the XML 1.0 standard, XML was born.

XML uses a simple and flexible standard formats, provides a Web-based application is an effective means of describing data and exchange data . However, XML is not used instead of HTML. HTML description focuses on how the file is displayed in the browser, it will focus on how the data is represented in a structured manner.

XML is simple and easy to read / write data in any application, which makes XML will soon become the only common language for data exchange, XML is widely used.

Note:  XML is a format for data exchange, not a programming language. And he is cross-language data format, the vast majority of programming languages support XML.

Examples 2.XML

XML exactly how to use? What is it like? Let's take a simple example!

A company is going to Company B docking operations (A company should obtain information about users of Company B), B Company provides an interface to make A's call, A, B Company butt developers can communicate well in advance of this interface: URL, parameter passing data is returned, exceptions, and so on.

But perhaps the two companies use technology stack is not the same, so the support of data formats may be different. In order to solve different due to the different technology stack to bring the issue of data formats, A, B company developed negotiate the use of a common data format for transmission, so they thought of XML.

  1. Suppose now that A company needs to call the user name information pig, then Company A calls B's interface and pass the parameter name = pig.
  2. Company B then the interface receives the request, the user information out from the database, and then packaged as the following XML format, and then back to the A company.
  3. A return to the company after receiving the final, you can parse the data using XML library
<?xml version="1.0" encoding="UTF-8"?>
<person>
  <name>pig</name>
  <age>18</age>
  <sex>man</sex>
  <hometown>
    <province>江西省</province>
    <city>抚州市</city>
    <county>崇仁县</county>
  </hometown>
</person>

3.XML crossroads

While the XML standard itself is simple, but the XML-related standards are a wide range, there are more than twenty of the W3C standards, adopted important standard XML e-commerce have developed more than a dozen. This gives software developers brought great trouble!

With AJax (previously called XMLHTTP, after 2005 called Ajax) popular technology, XML has become increasingly apparent drawbacks: We all know that implementation is DOM tree XML-based implementation, and DOM implemented in various browsers details vary, the XML cross-browser compatibility is not good , so the need for a new data payload format integrated into the HTML pages in order to meet the requirements of Ajax!

Two, JSON

Earlier we said that with the popularity of Ajax, and a variety of different browser DOM implementation details, so there will be compatibility problems, this really is a disaster for the students in terms of front-end development. Because a function might need to use the code compatible with a variety of different browsers, but also to debug, enormous workload.

1.JSON birth

How can integrate data into HTML and resolve browser compatibility problems? The answer is: the use of all major browsers in a general assembly --JavaScript engine . So long as to create a JavaScript engine can identify the data format can be friends!

In April 2001, the first JSON formatted message is sent out. This message was sent from a computer in a garage in the San Francisco Bay Area, which is important to the history of computing time. Douglas Crockford (Douglas Crockford) and  Chip Mourning Star (Chip Morningstar) is a company called State Software's technology consulting firm's co-founder (and later worked in both Yahoo), they were gathered in Morningstar's garage test an idea, sent this message.

document.domain = 'fudco'; 

parent.session.receive( 
    { to: "session", do: "test", text: "Hello world" } ) 

Js familiar with the students is not very surprised, first JSON messages it is obviously JavaScript! In fact, Crockford himself has said that he is not the first person to do so. Netscape (Netscape) company of someone on the use of JavaScript array literal as early as 1996 to exchange information. Because the news is JavaScript, it does not require any special parsing, JavaScript interpreter can get everything.

The initial information is actually JSON and JavaScript interpreter conflict. JavaScript retains a large number of keywords (ECMAScript 6 version will have 64 reserved word), Crockford and Morningstar inadvertently use a reserved word in its JSON in: do. Because JavaScript reserved word used too much, so Crockford decided: Since inevitable to use these reserved words, it would require all of the JSON key names in quotation marks . It is caused to be the key name is identified as a string JavaScript interpreter. That is why today JSON key reason for the name to be enclosed in quotation marks.

Here Insert Picture Description
This data format can now be JavaScript engine recognition, it would solve a variety of browser compatibility problems caused by XML, so this technology can promote out, so Crockford and Morningstar wanted to name it "JSML", represents JavaScript Markup language (JavaScript Markup language) meaning, but the acronym has been found that a thing called Java Speech Markup language being used. So they decided to use "JavaScript Object Notation", abbreviated to JSON, JSON so far officially born.

2.JSON development

In 2005, JSON had a big outbreak. That year, web designers and developers of a man named Jesse James Garrett coined the term "AJAX" in a blog post. He was careful to emphasize: AJAX is not a new technology, but "several technologies to flourish in some powerful new way to come together." AJAX is a newly developed Web applications Garrett to this being favored naming method. His blog article goes on to describe how developers can use JavaScript and XMLHttpRequest to build new applications, these applications more responsive and status than traditional web pages. He also Gmail and Flickr sites already using AJAX technology as an example.

Of course, "AJAX" in the "X" represents the XML. But in the subsequent question and answer post, Garrett pointed out, JSON can completely replace XML. He wrote: " Although XML is AJAX client data input, the most complete technology output, but to achieve the same effect, you can use JavaScript Object Notation (JSON) or any similar construction methods and other technical data like ."

Then it JSON abroad blog circle, circle slowly popular technology!

2006 年,Dave Winer,一位高产的博主,他也是许多基于 XML 的技术(如 RSS 和 XML-RPC)背后的开发工程师,他抱怨到 JSON 毫无疑问的正在重新发明 XML。

Crockford 阅读了 Winer 的这篇文章并留下了评论。为了回应 JSON 重新发明 XML 的指责,Crockford 写到:“重造轮子的好处是可以得到一个更好的轮子”。

3.JSON实例

还是以上面A、B公司业务对接为例子,两边的开发人员协商一种通用的数据交换格式,现在有XML与JSON比较流行的两种数据格式,于是开发人员又将用户信息以JSON形式展现出来,然后比较两种数据格式:

{
  "person": {
    "name": "pig",
    "age": "18", "sex": "man", "hometown": { "province": "江西省", "city": "抚州市", "county": "崇仁县" } } }

比较XML与JSON的数据格式之后,开发人员发现:JSON可阅读性、简易性更好而且相同数据负载JSON字符数更少,所以两个开发人员一致同意使用JSON作为接口数据格式!

而且还有重要的一点,在编写XML时,第一行需要定义XML的版本,而JSON不存在版本问题,格式永远不变!

4.当今JSON地位

当今的JSON 已经占领了全世界。绝大多数的应用程序彼此通过互联网通信时,都在使用 JSON。它已被所有大型企业所采用:十大最受欢迎的 web API 接口列表中(主要由 Google、Facebook 和 Twitter 提供),仅仅只有一个 API 接口是以 XML 的格式开放数据的。

JSON 也在程序编码级别和文件存储上被广泛采用:在 Stack Overflow上,关于JSON的问题越来越多,下图是关于Stack Overflow上不同数据交换格式的问题数和时间的曲线关系图。Here Insert Picture Description
从上图我们可以看出在Stack Overflow上越来越多JSON的问题,从这里也可以反映出JSON越来越流行!

三、总结

由于篇幅原因我们今天只学习了JSON的诞生和起源相关知识,知道了JSON的诞生是因为XML无法满足Ajax对浏览器兼容性问题,所以就有人想创造一种浏览器通用组件:JavaScript引擎 能识别的数据格式,这样就可以解决浏览器不兼容问题,所以就从Js数据格式中提取了一个子集,取名为JSON!

我们还知道了为什么JSON键为什么需要用双引号引起来,是因为JS中存在许多的关键字和保留关键字,为了避免与JS关键字冲突,所以Crockford就要求在所有的键名上加上双引号,这样JS引擎会将其识别为字符串,就避免与JS中关键字冲突!

下期我们会详细介绍JSON数据结构、JSON序列化、JSON在Python中的使用等知识。

了解技术诞生与发展背后的故事同样重要,因为这些可以作为你吹逼的资本!

 

JSON作为目前Web主流的数据交换格式,是每个IT技术人员都必须要了解的一种数据交换格式。尤其是在Ajax和REST技术的大行其道的当今,JSON无疑成为了数据交换格式的首选

今天我们一起来学习一下JSON的相关知识吧!
Here Insert Picture Description

一、XML

在讲JSON之前,我觉得有必要先带大家了解一下XML(Extensible Markup Language 可扩展标记语言),因为JSON正在慢慢取代XML。

1.XML起源

早期Web发展和负载的数据量并不是很大,所以基本靠HTML(1989诞生)可以解决。但是随着Web应用的不断壮大,HTML的一些缺点也慢慢显现,如:可读性差、解析时间长、数据描述性差等。

1998年2月10日,W3C(World WideⅥiebConsortium,万维网联盟)公布XML 1.0标准,XML诞生了。

XML使用一个简单而又灵活的标准格式,为基于Web的应用提供了一个描述数据和交换数据的有效手段。但是,XML并非是用来取代HTML的。HTML着重如何描述将文件显示在浏览器中,它着重描述如何将数据以结构化方式表示。

XML简单易于在任何应用程序中读/写数据,这使XML很快成为数据交换的唯一公共语言,所以XML被广泛应用。

注意: XML是一种数据交换的格式,并不是编程语言。而且他是跨语言的数据格式,目前绝大多数编程语言均支持XML。

2.XML实例

XML究竟怎么用?是什么样子的?我们来举一个简单的例子吧!

A公司要和B公司业务对接(A公司要获取B公司的用户基本信息),B公司提供接口让A公司调用,A、B公司对接的开发人员会提前沟通好这个接口的:URL、传参、返回数据、异常等等。

但是也许两个公司使用的技术栈并不相同,所以支持的据格式也可能不同。为了解决因技术栈不同带来的数据格式不同问题,A、B公司的开发协商使用一种通用的数据格式来传输,于是他们想到了XML。

  1. 假设现在A公司需要名称叫pig的用户信息,于是A公司调用B公司的接口,并传参数name=pig。
  2. 然后B公司接口收到请求后,将用户信息从数据库拿出来,然后封装成下面的XML格式,然后再返回给A公司。
  3. 最后A公司收到返回后,使用XML库解析数据即可
<?xml version="1.0" encoding="UTF-8"?>
<person>
  <name>pig</name>
  <age>18</age>
  <sex>man</sex>
  <hometown>
    <province>江西省</province>
    <city>抚州市</city>
    <county>崇仁县</county>
  </hometown>
</person>

3.XML十字路口

虽然XML标准本身简单,但与XML相关的标准却种类繁多,W3C制定的相关标准就有二十多个,采用XML制定的重要的电子商务标准就有十多个。这给软件开发工程师带来了极大的麻烦!

随着AJax(之前叫XMLHTTP,2005年后才叫Ajax)技术的流行,XML的弊端也越来越显现:大家都知道XML实现是基于DOM树实现的,而DOM在各种浏览器中的实现细节不尽相同,所以XML的跨浏览器兼容性并不好,所以急需一种新的数据负载格式集成到HTML页面中以满足Ajax的要求!

二、JSON

前面我们说了随着Ajax的流行,而各种浏览器对DOM的实现细节不尽相同,所以会出现兼容性问题,这对前端开发同学来讲真的是灾难。因为一个功能可能需要用代码去兼容各种不同的浏览器,还要调试,工作量巨大。

1.JSON诞生

如何才能将数据整合到HTML中又解决浏览器兼容性问题呢?答案就是:利用所有主流浏览器中的一种通用组件——JavaScript引擎。这样只要创造一种JavaScript引擎能识别的数据格式就可以啦!

2001 年 4 月,首个 JSON 格式的消息被发送出来。此消息是从旧金山湾区某车库的一台计算机发出的,这是计算机历史上重要的的时刻。道格拉斯·克罗克福特(Douglas Crockford) 和 奇普·莫宁斯达(Chip Morningstar) 是一家名为 State Software 的技术咨询公司的联合创始人(后来都在雅虎任职),他们当时聚集在 Morningstar 的车库里测试某个想法,发出了此消息。

document.domain = 'fudco'; 

parent.session.receive( 
    { to: "session", do: "test", text: "Hello world" } ) 

熟悉js的同学是不是也很惊讶,第一个 JSON 消息它明显就是 JavaScript!实际上,Crockford 自己也说过他不是第一个这样做的人。网景(Netscape )公司的某人早在 1996 年就使用 JavaScript 数组字面量来交换信息。因为消息就是 JavaScript,其不需要任何特殊解析工作,JavaScript 解释器就可搞定一切。

最初的 JSON 信息实际上与 JavaScript 解释器发生了冲突。JavaScript 保留了大量的关键字(ECMAScript 6 版本就有 64 个保留字),Crockford 和 Morningstar 无意中在其 JSON 中使用了一个保留字:do。因为 JavaScript 使用的保留字太多了,所以Crockford决定:既然不可避免的要使用到这些保留字,那就要求所有的 JSON 键名都加上引号。被引起来的键名会被 JavaScript 解释器识别成字符串。这就为什么今天 JSON 键名都要用引号引起来的原因。

Here Insert Picture Description
这种数据格式既然可以被JavaScript引擎识别,那就解决了XML带来的各种浏览器兼容性问题,所以这种技术完全可以推广出去,于是Crockford 和 Morningstar 想给其命名为 “JSML”,表示JavaScript 标记语言(JavaScript Markup Language)的意思,但发现这个缩写已经被一个名为 Java Speech 标记语言的东西所使用了。所以他们决定采用 “JavaScript Object Notation”,缩写为 JSON,至此JSON正式诞生。

2.JSON发展

2005 年,JSON 有了一次大爆发。那一年,一位名叫 Jesse James Garrett 的网页设计师和开发者在博客文章中创造了 “AJAX” 一词。他很谨慎地强调:AJAX 并不是新技术,而是 “好几种蓬勃发展的技术以某种强大的新方式汇集在一起。” AJAX 是 Garrett 给这种正受到青睐的 Web 应用程序的新开发方法的命名。他的博客文章接着描述了开发人员如何利用 JavaScript 和 XMLHttpRequest 构建新型应用程序,这些应用程序比传统的网页更具响应性和状态性。他还以 Gmail 和 Flickr 网站已经使用 AJAX 技术作为了例子。

当然了,“AJAX” 中的 “X” 代表 XML。但在随后的问答帖子中,Garrett 指出,JSON 可以完全替代 XML。他写道:“虽然 XML 是 AJAX 客户端进行数据输入、输出的最完善的技术,但要实现同样的效果,也可以使用像 JavaScript Object Notation(JSON)或任何类似的结构数据方法等技术。 ”

这时JSON便在国外的博客圈、技术圈慢慢流行起来!

2006 年,Dave Winer,一位高产的博主,他也是许多基于 XML 的技术(如 RSS 和 XML-RPC)背后的开发工程师,他抱怨到 JSON 毫无疑问的正在重新发明 XML。

Crockford 阅读了 Winer 的这篇文章并留下了评论。为了回应 JSON 重新发明 XML 的指责,Crockford 写到:“重造轮子的好处是可以得到一个更好的轮子”。

3.JSON实例

还是以上面A、B公司业务对接为例子,两边的开发人员协商一种通用的数据交换格式,现在有XML与JSON比较流行的两种数据格式,于是开发人员又将用户信息以JSON形式展现出来,然后比较两种数据格式:

{
  "person": {
    "name": "pig",
    "age": "18", "sex": "man", "hometown": { "province": "江西省", "city": "抚州市", "county": "崇仁县" } } }

比较XML与JSON的数据格式之后,开发人员发现:JSON可阅读性、简易性更好而且相同数据负载JSON字符数更少,所以两个开发人员一致同意使用JSON作为接口数据格式!

而且还有重要的一点,在编写XML时,第一行需要定义XML的版本,而JSON不存在版本问题,格式永远不变!

4.当今JSON地位

当今的JSON 已经占领了全世界。绝大多数的应用程序彼此通过互联网通信时,都在使用 JSON。它已被所有大型企业所采用:十大最受欢迎的 web API 接口列表中(主要由 Google、Facebook 和 Twitter 提供),仅仅只有一个 API 接口是以 XML 的格式开放数据的。

JSON 也在程序编码级别和文件存储上被广泛采用:在 Stack Overflow上,关于JSON的问题越来越多,下图是关于Stack Overflow上不同数据交换格式的问题数和时间的曲线关系图。Here Insert Picture Description
从上图我们可以看出在Stack Overflow上越来越多JSON的问题,从这里也可以反映出JSON越来越流行!

三、总结

由于篇幅原因我们今天只学习了JSON的诞生和起源相关知识,知道了JSON的诞生是因为XML无法满足Ajax对浏览器兼容性问题,所以就有人想创造一种浏览器通用组件:JavaScript引擎 能识别的数据格式,这样就可以解决浏览器不兼容问题,所以就从Js数据格式中提取了一个子集,取名为JSON!

我们还知道了为什么JSON键为什么需要用双引号引起来,是因为JS中存在许多的关键字和保留关键字,为了避免与JS关键字冲突,所以Crockford就要求在所有的键名上加上双引号,这样JS引擎会将其识别为字符串,就避免与JS中关键字冲突!

The next issue we will detail JSON data structures, JSON serialization, JSON and other knowledge to use in Python.

Learn the story behind the birth and development of technology is equally important, because these can be used as you blow forced the capital!

Guess you like

Origin www.cnblogs.com/IT-Evan/p/JSON.html