This article mainly introduces the objects and JSON in JavaScript. This article explains how to define JSON and javascript objects, convert javascript objects to JSON, and parse JSON in javascript. For more information, see
Introduction
JSON is JavaScript Object Natation. It is a lightweight data exchange format and is very suitable for server-to-JavaScript interaction.
JSON is a data exchange format. Like XML and YAML, JSON is a way to transmit structured information between different languages. On the other hand, javascript objects are a data type in javascript language, just like arrays in PHP, classes and struct in C ++.
Define JSON and javascript objects
When defining an object in a javascript program, the property name of the object can be added with double quotation marks or without double quotation marks. If the attribute name contains special characters (such! , If, etc.), you must add double quotation marks.
When defining JSON, the attribute name must be enclosed in double quotation marks.
Sample Code:
1. Define javascript objects
The Code is as follows:
Var obj = {name: "tudouya", "sex": "man"}; # two attributes can be enclosed in double quotation marks or not added.
Var obj = {"! ":" Hello world "}; # Double quotation marks are required when the attribute name contains special characters.
2. Define a JSON string
The Code is as follows:
Var jsonString = {"name": "tudouya"}; # Double quotation marks must be added when defining JSON.
Convert javascript objects to JSON
1. Convert javascript objects to JSON
We can use the built-in functions of javascript to convert javascript objects to JSON. This function is JSON. stringify ().
Sample Code:
The Code is as follows:
Var obj = {name: "tudouya", sex: "man "};
Var jsonObj = JSON. stringify (obj );
Console. log (jsonObj );
# Output result: {"name": "tudouya", "sex": "man "}
When converting javascript objects to JSON, note the following:
If an object contains attributes whose values are functions and dates, JSON ignores the attributes whose values are functions and converts the attributes whose values are dates to strings.
Sample Code:
The Code is as follows:
Var obj = {
Name: "tudouya ",
Birthday: new Date (),
Action: function (){
Document. write ("walk ");
}
};
Var jsonObj = JSON. stringify (obj );
Console. log (jsonObj );
# Output result: {"name": "tudouya", "birthday": "2014-08-12T10: 05: 00.497Z "}
Parse JSON in javascript
In older versions of JS, we usually use the eval () function to parse JSON, but ECMAScript5 provides us with a new JSON-parsing function, JSON. parse ().
This function is easy to use and you can try it on your own. After applying the function to a JSON string, the JSON is converted to a javascript Object. That is to say, when the typeof operator is used to view the type of the function, the returned value is the Object.
It is also worth noting that this function is supported only after ECMAScript5. If it is an old version browser, it may not be supported. The solution is to load a js file that implements the function, namely, json2.js. If the JQuery framework is used, jQuery. parseJSON (), this function calls the JSON. parse () method.
For how to parse JSON using the eval () method, this will be recorded after deep learning.
A very important concept
As a front-end cainiao, I often hear people say "JSON objects", but there is no such concept as "JSON objects". The real form of JSON is a string.