[Go] data type and JSON format

Source: Internet
Author: User
Tags php language scalar vcard

Nanyi

Date: May 30, 2009

1.

A few days ago, I realized that there was a simplified data interchange format called YAML.

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

It says that, structurally, all data can eventually be decomposed into three types :

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

The second type is the sequence (sequence), which is a number of related data that are tied together in a certain order, also called an array or list, such as "Beijing, Shanghai."

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

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

2.

I immediately thought of JSON.

At the beginning of 21st century, Douglas Crockford looked for a simple data interchange format that could exchange data between servers. The universal data Exchange language was XML at the time, but Douglas Crockford thought that XML generation and parsing were too cumbersome, so he proposed a simplified format, json.

JSON specifications are very simple, with only one page hundreds of words can be said clearly, and Douglas Crockford claims that this specification never need to upgrade, because the provisions of the provision.

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

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

3) a set (array) of side data is expressed in square brackets ("[]").

4) The Set of Mappings (objects) are represented by curly braces ("{}").

The above four rules are all content in JSON format.

For example, the following sentence:

"Beijing's area is 16800 square kilometers, the resident population is 16 million people." The area of Shanghai is 6400 square kilometers, the resident population is 18 million. "

Written in 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 use. So, in just a few years, it replaced XML as the most popular data interchange format on the Internet.

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

3.

When I was learning JavaScript, I once had no idea where the fundamental difference between array and object is, and both can be used to represent a collection of data.

For example, there is an array a=[1,2,3,4], there is an object A={0:1,1:2,2:3,3:4}, and then you run alert (a[1]), the results of the operation are the same in both cases! This means that the data collection can be represented either as an array or as an object, so which one 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 will use the object.

4.

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

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

In JavaScript, for example, 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 my confusion with arrays and objects, and later realized that in the JavaScript language, an associative array is an object, an object is an associative array. This is completely different from the PHP language, where associative arrays are arrays as well.

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 array A is 4.

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

<?php

$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 array A is 5.

Finish

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

[Go] data type and JSON format

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.