Javascript study notes (vi) data types and JSON format

  • 2020-03-30 04:02:17
  • OfStack

What is a JSON

JSON: JavaScript Object Notation.

JSON is in the form of a list of items surrounded by braces "{}," separated by a comma (,), and an item is a property name and property value separated by a colon (:). This is a typical dictionary representation, and again shows that objects in javascript are dictionary structures. No matter how complex an object is, it can be created and assigned with a single JSON code.

JSON structure

JSON has two structures

Json is simply an object and an array in javascript, so these two structures are the object and array structures, which represent a variety of complex structures

1. Object: the object is represented in js as the contents enclosed by "{}", and the data structure is {key: value,key: value... In object-oriented languages, the key is the property of the object, and the value is the corresponding property value, so it is easy to understand, the value method is the object.

2, array: array in js is the bracket "[]" enclosed by the content, the data structure is [" Java ","javascript","vb"...] , as in all languages, the value of the use of the index, the type of field value can be Numbers, strings, arrays, several objects.
Through the object, array 2 structures can be combined into a complex data structure.

JSON syntax rules

JSON syntax is a subset of JavaScript object notation syntax.

The data is in the name/value pair
Data is separated by commas
Curly braces hold the object
Square brackets hold the array
The JSON value can be:

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

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

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

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

4) the set (object) of the map is represented by braces ("{}").
JSON example

Create an object without any properties:


var obj = {};

Create an object and set the properties and initial values:


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

Create an object and set properties and methods:


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

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


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 value, a true or false, an array, or an object

Data type:

Structurally, all data can eventually be decomposed into three types:

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

The second type is a sequence, in which several related pieces of data are placed side by side in a certain order, also known as an array or List, such as "Beijing, Shanghai".

The third type is mapping, or Name/value, where the data has a Name and a corresponding value, also known as a hash or dictionary, such as "capital: Beijing."
In a programming language, as long as you have an array and an object, you can store everything.

Another difference between an array and an object is that the array data has no "name" (name), whereas the object data has a "name" (name).

There are five simple data types (also known as primitive data types) in JavaScript: Undefined, Null, Boolean, Number, and String. There is also a complex data type -- Object, which is essentially an unordered set of name-value pairs.

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

low "undefined" -- if the value is undefined;

low "Boolean" -- if the value is Boolean;

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

low "number" -- if the value is a number;

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

low "function" -- if this value is a function;

Undefined type:

      The 'Undefined' type has only one value, and when a variable is declared with var but not initialized,
The value of this variable is undefined
Null type

A Null type is the second data type that has only one value, and this particular value is Null. Logically, a null value represents an empty object pointer, which is why the typeof operator returns "object" when it detects null, for example:


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

The Number type

This type is used to represent integers and floating point values, as well as a special type of value, NaN (Not a Number). This value is used to indicate that an operand that was intended to return a value does not return a value (so that an error is not thrown).

Type String

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

Most of the time, you don't have to pass an argument to call the toString() method. However, when the toString() method of a numeric value is called, one parameter can be passed: the cardinality of the output value.


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"

You can also use the transform function String(), which converts any type of value to a String, without knowing whether the value to be converted is null or undefined. The String() function follows the following conversion rules:

low if the value has a toString() method, the method is called (with no arguments) and the corresponding result is returned

low returns "null" if the value is null

low if the value is undefined, return "undefined"

The Object type

An object is simply a collection of data and functionality. An object can be created by executing the new operator followed by the name of the object type to be created. By creating an instance of type Object and adding properties and/or methods to it, you can create a custom Object.

Var o = new Object();
The typeof operator


<script type="text/javascript">
    var s = "Nicholas";
    var b =true;
    var c = 21;
    var u;
    var n = null;
    var o = {};
    var obj = new Object;//It's best to write new object()     alert(typeof s);//string
    alert(typeof b);//boolean
    alert(typeof c);//number
    alert(typeof u);//undefined
    alert(typeof n);//object
    alert(typeof o);//object
    alert(typeof obj);//object
</script>

Json online parsing


Related articles: