Home Web Front-end JS Tutorial JavaScript study notes (6) Data types and JSON format_Basic knowledge

JavaScript study notes (6) Data types and JSON format_Basic knowledge

May 16, 2016 pm 04:34 PM
javascript json format type of data

What is JSON

JSON: JavaScript Object Notation.

The format of JSON is a list of items surrounded by curly brackets "{}", each item is separated by a comma (,), and the item is the attribute name and attribute value separated by a colon (:). This is a typical dictionary representation, and it once again shows that objects in JavaScript are dictionary structures. No matter how complex the object is, it can be created and assigned with a JSON code.

JSON structure

JSON has two structures

Json simply means objects and arrays in JavaScript, so these two structures are objects and arrays. Various complex structures can be expressed through these two structures

1. Object: The object is represented in js as the content enclosed by "{}". The data structure is the key-value pair structure of {key: value, key: value,...}. In object-oriented language , key is the attribute of the object, and value is the corresponding attribute value, so it is easy to understand. The value method is object.key to obtain the attribute value. The type of this attribute value can be numbers, strings, arrays, and objects.

2. Array: Array in js is the content enclosed by brackets "[]", the data structure is ["java", "javascript", "vb",...], the value method and all languages Same as in, using index to obtain, the type of field value can be number, string, array, object.
Through the two structures of objects and arrays, complex data structures can be combined.

JSON syntax rules

JSON syntax is a subset of JavaScript Object Notation syntax.

Data in name/value pairs
Data separated by commas
Curly braces save objects
Square brackets save arrays
JSON value can be:

Number (integer or floating point)
String (in double quotes)
Logical value (true or false)
Array (in square brackets)
Object (in curly braces)
null

1) Parallel data are separated by commas (", ").

2) Mapping is represented by colon (": ").

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

4) The mapped collection (object) is represented by curly brackets ("{}").
JSON example

Create an object without any properties:

Copy code The code is as follows:

var obj = {};

Create an object and set properties and initial values:

Copy code The code is as follows:

var author = {name: "trigkit4", age: 21, sex: "male"};

Create an object and set properties and methods:

Copy code The code is as follows:

var hello ={content:"how are you?" , say :function(){alert(this.content)} };

Create a nested array of other objects, objects, etc.:

Copy code The code is as follows:

var company = {name:"Apple",
Product: "iPPhone",
Chairman:{name:"Tim Cook",age:54},
Employees:[{name:"Jony Ive",age:47},{name:"Lili",age:29}],
};

An object is an unordered set of name/value pairs. An object starts with the left branch and ends with the right branch


A value can be a string enclosed in double quotes, or a numeric value, a true or false, an array or an object

Data type:

From a structural point of view, all data can ultimately be decomposed into three types:

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

The second type is sequence, that is, several related data are arranged together in a certain order, also called array or list, such as "Beijing, Shanghai".

The third type is mapping, which is a name/value pair, that is, the data has a name and a corresponding value, which is also called hash. ) or dictionary, such as "Capital: Beijing".
In programming languages, as long as there are arrays and objects, all data can be stored.

Another difference between arrays and objects is that array data does not have a "name", while object data has a "name".

There are 5 simple data types (also called basic data types) in JavaScript: Undefined, Null, Boolean, Number and String. There is also a complex data type - Object. Object is essentially composed of a set of unordered name-value pairs.

Using the typeof operator on a value may return one of the following strings:

 ● "undefined" - if the value is undefined;

 ● "boolean" - if the value is a Boolean value;

 ● "string" - if the value is a string;

 ● "number" - if the value is a numerical value;

 ● "object" - if the value is an object or null;

 ● "function" - if the value is a function;

Undefined type:

The `Undefined` type has only one value. When using var to declare a variable but not initialize it,
The value of this variable is undefined
Null type

The Null type is the second data type with only one value, and this special value is null. From a logical point of view, the null value represents a null object pointer, and this is why "object" is returned when using the typeof operator to detect null, for example:

Copy code The code is as follows:

var car = null;
alert(typeof car); // "object"

Number type

 This type is used to represent integers and floating-point values, and there is also a special value, NaN (Not a Number). This value is used to indicate that an operand that is supposed to return a value does not return a value (so that an error is not thrown).

String type

The String type is used to represent a character sequence consisting of zero or more 16-bit Unicode characters, that is, a string. Strings can be represented by single quotes (') or double quotes (").
Numeric, Boolean, object and string values ​​all have toString() method. But null and undefined values ​​do not have this method.

In most cases, there is no need to pass parameters when calling the toString() method. However, when calling the toString() method of a value, you can pass a parameter: the base of the output value.

Copy code The code is as follows:

var num = 10;
alert(num.toString()); //"10"
alert(num.toString(2)); //"1010"
alert(num.toString(8)); //"12"
alert(num.toString(10)); //"10"
alert(num.toString(16)); //"a"

When you don’t know whether the value to be converted is null or undefined, you can also use the conversion function String(). This function can convert any type of value into a string. The String() function follows the following conversion rules:

 ● If the value has a toString() method, call this method (without parameters) and return the corresponding result

 ● If the value is null, return "null"

 ● If the value is undefined, return "undefined"

Object type

An object is actually a collection of data and functions. Objects can be created by executing the new operator followed by the name of the type of object to be created. You can create a custom object by creating an instance of the Object type and adding properties and/or methods to it.

var o = new Object();
typeof operator

Copy code The code is as follows:


Json online parsing

Json online analysis: http://json.tongxiehui.net/

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the best data type for gender fields in MySQL? What is the best data type for gender fields in MySQL? Mar 15, 2024 am 10:24 AM

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

What data type should be used for gender field in MySQL database? What data type should be used for gender field in MySQL database? Mar 14, 2024 pm 01:21 PM

In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Python entry to proficiency: from zero basics to project development Python entry to proficiency: from zero basics to project development Feb 20, 2024 am 11:42 AM

1. Introduction to Python Python is a general-purpose programming language that is easy to learn and powerful. It was created by Guido van Rossum in 1991. Python's design philosophy emphasizes code readability and provides developers with rich libraries and tools to help them build various applications quickly and efficiently. 2. Python basic syntax The basic syntax of Python is similar to other programming languages, including variables, data types, operators, control flow statements, etc. Variables are used to store data. Data types define the data types that variables can store. Operators are used to perform various operations on data. Control flow statements are used to control the execution flow of the program. 3.Python data types in Python

What is the best data type choice for gender field in MySQL? What is the best data type choice for gender field in MySQL? Mar 14, 2024 pm 01:24 PM

When designing database tables, choosing the appropriate data type is very important for performance optimization and data storage efficiency. In the MySQL database, there is really no so-called best choice for the data type to store the gender field, because the gender field generally only has two values: male or female. But for efficiency and space saving, we can choose a suitable data type to store the gender field. In MySQL, the most commonly used data type to store gender fields is the enumeration type. An enumeration type is a data type that can limit the value of a field to a limited set.

Detailed explanation of how to use Boolean type in MySQL Detailed explanation of how to use Boolean type in MySQL Mar 15, 2024 am 11:45 AM

Detailed explanation of how to use Boolean types in MySQL MySQL is a commonly used relational database management system. In practical applications, it is often necessary to use Boolean types to represent logical true and false values. There are two representation methods of Boolean type in MySQL: TINYINT(1) and BOOL. This article will introduce in detail the use of Boolean types in MySQL, including the definition, assignment, query and modification of Boolean types, and explain it with specific code examples. 1. The Boolean type is defined in MySQL and can be

What are the data types of java What are the data types of java Jan 30, 2024 pm 03:23 PM

Java data types: 1. Integer type; 2. Floating point type; 3. Character type; 4. Boolean type; 5. Other data types; 6. Reference types; 7. Primitive types and encapsulated classes; 8. Autoboxing and Unboxing; 9. Variable parameters; 10. Annotations; 11. Enumeration; 12. Selection of primitive types and reference types. Java is a strongly typed language, so every data has its fixed type.

See all articles