Data storage type and JSON

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 (scalar), which is a separate string or number (numbers), such as the word "Beijing. The second type is sequence (sequence), that is, several related data are listed in a certain order, also called array or List, for example, "Beijing, tokyo ". The third type is mapping, that is, a Name/value pair (Name/value), that is, the data has a Name and a value corresponding to it, this 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 arrays and objects are available, 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. This actually requires two steps. The first step is to convert all kinds of data into a string, that is, serialization of data. The second step is to exchange this string.

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 with only one page and several hundred words, and Douglas Crockford claims that this specification will never need to be upgraded because it is stipulated in all the provisions.

  • Separated by commas.
  • The ing is represented by a colon.
  • The Set (array) of the parallel data is represented by square brackets.
  • 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:

[{"City": "Beijing", "area": 16800, "Population": 1600 },{ "city": "Shanghai", "area": 6400, "Population": 1800}]

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

[["Beijing", 16800,1600], ["Shanghai", 6400,1800]

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!

I still remember that when I was learning javascript, I was confused about the fundamental differences between "array" and "object, both of them can be used to represent a set 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 data in the array does not have a "name" and the data in the object has a "name ).

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

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

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

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

A ["city"] = "Beijing"; a ["area"] = 16800 square meters; a ["Population"] = 1600 square meters;

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:

var a=[1,2,3,4];a['foo']='Hello World';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:

$a=array(1,2,3,4);$a["foo"]="Hello world";echo count($a);  

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

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.