Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand data types and Json format

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to understand data types and Json format, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

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

I flipped through its documents and didn't understand much, but I was enlightened by a sentence.

Structurally, it says, all data (data) can eventually be broken down into three types:

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

The second type is sequence, in which several related data are juxtaposed in a certain order, also known as array or List, such as "Beijing, Shanghai".

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

It suddenly dawned on me that the smallest unit of data is so simple! No wonder in programming languages, as long as you have array and object, you can store all the data.

I immediately thought of json.

At the beginning of the 21st century, Douglas Crockford looked for a simple data exchange format that could exchange data between servers. At that time, the general data exchange language was XML, but Douglas Crockford found it too troublesome to generate and parse XML, so he proposed a simplified format, that is, Json.

The specification of Json is so simple that it can be made clear in only a few hundred words on a page, and Douglas Crockford claims that the specification never needs to be upgraded because it is stipulated.

1) the juxtaposed data is separated by a comma (",").

2) the mapping is indicated by a colon (":").

3) A collection (array) of juxtaposed data is represented by square brackets ("[]").

4) the mapped set (object) is represented by curly braces ("{}").

The above four rules are all the content in Json format.

For example, the following sentence:

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

This is what happens when written in json format:

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

If you know the structure of the data in advance, the above writing can be further simplified:

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

From this we can see that json is very easy to learn and use. So, in just a few years, it replaced xml as the data exchange format on the Internet.

I guess Douglas Crockford must have known in advance that data structures can be simplified into three forms, otherwise how could json be so refined!

When I was learning javascript, I was confused about the fundamental difference between "array" and "object", both of which can be used to represent collections of data.

For example, there is an array a = [1gime 2je 3je 4], and there is an object a = {0range1jue 1ju 2ju 2ju 3je 3j4}, and then you run alert (a [1]), and the result is the same in both cases! That is to say, data sets can be represented by either arrays or objects, so which one should I use?

I learned later that arrays represent collections of ordered data, while objects represent collections of unordered data. If the order of the data is important, use arrays, otherwise use objects.

Of course, another difference between an array and an object is that the data of the array does not have a "name" (name), while the data of the object 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 has a name.

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 associative array:

A ["city"] = "Beijing"; a ["area"] = 16800; a ["population"] = 1600

This also increased my confusion about arrays and objects at first, but later I realized that in the Javascript language, associative arrays are objects, and objects are associative arrays. This is completely different from the php language, where associative arrays are also arrays in php.

For example, run the following javascript:

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

The result of * * is 4, that is, the number of elements of the array an is 4.

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

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

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report