Data Type and Json format

Source: Internet
Author: User

A few days ago, I learned that there was a simplified data exchange format called yaml.

I have reviewed its documents, but I don't know much about it, but one sentence makes me feel confused.

In terms of structure, all data can be divided into three types:

The first type is scalar ),That is, a separate string) or number numbers, such as the word "Beijing.

The second type is sequence ),That is, several related data are listed in a certain order, also called array) or List), such as "Beijing, Shanghai ".

The third type is ing ),That is, a Name/value pair Name/value), that is, the data has a Name, and a value corresponding to it, which is also called hash) or dictionary ), for example, "capital: Beijing ".

I suddenly realized that the smallest unit of data was so simple! It is no wonder that in programming languages, as long as there is an array) and an object), all data can be stored.

I immediately thought of json.

In the early 21st century, Douglas Crockford looked for a simple data exchange format that could exchange data between servers. At that time, the Common Data Exchange Language was XML, but Douglas Crockford thought that XML generation and parsing were too troublesome, so he proposed a simplified format, namely Json.

The Json specification is very simple. It can be clearly stated in just a few hundred words on a page, and Douglas Crockford claims that this specification will never need to be upgraded, because it is stipulated in this provision.

1) separated by commas.

2) The ing is represented by the colon.

(3) the array of the parallel data set) is represented by square brackets.

4) The ing set object is represented by braces.

The above four rules are all in Json format.

For example, the following sentence:

"Beijing has an area of 16800 square kilometers and a resident population of 16 million people. Shanghai has an area of 6400 square kilometers and a resident population of 18 million. "

The json format is as follows:

 
 
  1. [
  2. {"City": "Beijing", "area": 16800, "Population": 1600 },
  3. {"City": "Shanghai", "area": 6400, "Population": 1800}
  4. ]

If you know the data structure in advance, the preceding statement can be further simplified:

 
 
  1. [
  2. ["Beijing", 16800,1600],
  3. ["Shanghai", 6400,
  4. ]

Json is easy to learn and use. Therefore, in just a few years, it has replaced xml and become the most popular data exchange format on the Internet.

I guess Douglas Crockford must have known in advance that the data structure can be simplified into three forms. Otherwise, how can we define json as so refined!

When I was learning javascript, I once did not know the fundamental difference between "array" and "object". Both of them can be used to represent a collection of data.

For example, there is an array a = [1, 2, 4], and an object a = {,}. Then you run alert (a [1]), in both cases, the running results are the same! This means that the data set can be represented by arrays or objects. Which one should I use?

I later learned that arrays represent the set of ordered data, while objects represent the set of unordered data. If the order of data is important, use an array. Otherwise, use an object.

Of course, another difference between an array and an object is that the array data does not have a "name"), and the object data has a "name" name ).

But the problem is that in many programming languages, there is something called "associative array" associative array. The data in this array is named.

For example, in javascript, you can define an object as follows:

 
 
  1. Var a = {"city": "Beijing", "area": 16800, "Population": 1600 };

However, it can also be defined as an associated array:

 
 
  1. A ["city"] = "Beijing ";
  2. A ["area"] = 16800;
  3. A ["Population"] = 1600;

At first, this increased the confusion between arrays and objects. Later, I realized that in Javascript, the associated array is the object, and the object is the associated array. This is completely different from the php language. In php, the associated array is also an array.

For example, run the following javascript code:

 
 
  1. var a=[1,2,3,4];  
  2. a['foo']='Hello World';  
  3. alert(a.length); 

The final result is 4, that is, the number of elements in array a is 4.

However, php code running the same content is different:

 
 
  1. <?php  
  2. $a=array(1,2,3,4);  
  3. $a["foo"]="Hello world";  
  4. echo count($a);  
  5. ?> 

The final result is 5, that is, the number of elements in array a is 5.

Original article address:Http://www.ruanyifeng.com/blog/2009/05/data_types_and_json.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.