Data types and JSON format Analysis Summary _ Related tips

Source: Internet
Author: User
Tags arrays php language php code scalar serialization

1.

A few days ago, I learned that there is a simplified format for data interchange, called Yaml.

I rummaged through its documents , but there was not much to see, but there was a word that made me enlightened.

It says that, structurally, all data can eventually be divided into three categories :

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

The second type is sequence (sequence), where several related data are tied together in a certain order, also called Array (array) or list (lists), such as "Beijing, Tokyo".

The third type is mapping (mapping), a name/value pair (Name/value), where the data has a name and a corresponding value, which is also known as hash (hash) or dictionary (dictionary), such as "Capital: Beijing".

It dawned on me that the smallest unit of data composition was simple! No wonder in programming languages, with arrays and objects (object), you can store all of your data.

2.

I immediately thought of JSON.

At the beginning of 21st century, Douglas Crockford was looking for a convenient data interchange format that could exchange data between servers. This actually takes two steps, and the first step is to convert the data into a string, the serialization of the data (serialization), and the second step is to exchange the string.

At that time, the universal Data Exchange language was XML, but Douglas Crockford thought that XML generation and parsing were too cumbersome, so he proposed a simplified format, that is, JSON.

The specification of JSON is very simple, only one page, hundreds of words can be clear, and Douglas Crockford claims that this specification never need to upgrade, because the provisions are.

1 The data is separated by commas (",").

2) The mapping is represented by a colon (":").

3 the Set (array) of parallel data is represented by square brackets ("[]").

4 the Mapped collection (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 kilometres and a permanent population of 16 million people." The area of Shanghai is 6400 square kilometers and the resident population is 18 million. ”

This is what you write in the JSON format:

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

If you know the structure of the data beforehand, the above wording can be further simplified:

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

As you can see, JSON is easy to learn and easy to use. So, in a few short years, it replaces XML as the most popular format for data interchange on the Internet.

I suspect that Douglas Crockford must have known beforehand that the data structure could be simplified into three forms, otherwise how could the JSON be so refined!

3.

I remember when I was learning JavaScript, I couldn't figure out exactly where the fundamental difference between "array" and "object" was, both of which could be used to represent a collection of data.

For example, there is an array a=[1,2,3,4], there is also an object A={0:1,1:2,2:3,3:4}, and then you run alert (a[1), the results are the same in both cases! That is to say, a data set can be expressed either as an array or an object, so what should I use?

I later learned that an array represents a collection of ordered data, whereas an object represents a collection of unordered data. If the order of the data is important, use an array, or you can use an object.

4.

Of course, another difference between an array and an object is that the data in the array has no name, and the data in the object has name.

But the problem is that in many programming languages, there is something called an associative array (associative array). The data in this array has a name.

For example, in JavaScript, you can define an object like this:

var a={"City": "Beijing", "area": 16800, "Population": 1600};

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

a["City"]= "Beijing";
a["Area"]=16800;
a["Population"]=1600;

This at first exacerbated the confusion between my arrays and objects, and it became clear that in the JavaScript language, associative arrays are objects, and objects are associative arrays. This is completely different from the PHP language, in PHP, associative arrays are also arrays.

For example, run the following javascript:

var a=[1,2,3,4];

a[' foo ']= ' Hello world ';

alert (a.length);

The final result is 4, which means that the number of elements in the array A is 4.

However, the PHP code that runs the same content is different:

$a =array (1,2,3,4);

$a ["foo"]= "Hello World";

echo count ($a);

The final result is 5, which means that the number of elements in the 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.