The application of JSON in JavaScript and its own understanding

Source: Internet
Author: User
Tags tojson

"Objects and JSON"
Json:javascript Object Notation (JavaScript object Notation).
JSON is the syntax for storing and exchanging textual information. Similar to XML.
JSON is a data format, not a programming language. There is no comment in JSON. JSON represents a data transfer format.
One, the syntax of JSON can represent the following three types of values: (Data structure)
(1) Simple value: Is the simplest form of data, which can be a string, a numeric value, a Boolean, a null. JSON does not support special value undefined in JavaScript. Note: Strings in JSON must be enclosed in double quotation marks.
(2) object: The object in JSON is slightly different from the JavaScript literal, but the object in the JSON requires the attribute to be quoted (double quotation marks), followed by a semicolon with no end (because he is not a JavaScript statement), which is the structure of the key-value pairs contained in curly braces {}.

For example:

The object literal in JavaScript can be:         var object = {               name: "Bob",               age:29              };         The above objects are represented in JSON in the following way:              {                  "name": "Bob",                  "age":              

(3) Arrays: Arrays are the second complex data types of JSON. That is, the "[]" starting to represent the elements separated by commas. The JSON array also has no variables and semicolons. Combining arrays and objects can constitute more complex collections of data.

Second, JSON parsing and serialization
JSON can parse JSON data structures into useful JavaScript objects. Is the fact standard of data exchange in Web development.

(1) JSON object: JSON is a subset of JavaScript syntax, so the eval () function can parse, interpret, and return JavaScript objects and arrays, but at a risk.
There are two methods of JSON objects: Stringify () and parse (). In the simplest case, these two methods serialize the JavaScript object to a JSON string and parse the JSON string into the native JavaScript value.
(2) Serialization options
Json.stringify () In addition to the JavaScript to serialize, you can also accept two additional parameters that specify a different way of serializing a JavaScript object. The first parameter is a filter, which can be an array or a function, and the second argument is an option that indicates whether to remain indented in the JSON string.
1. Filter Result: If the filter parameter is an array, then the result of Json.stringify () will contain only the attributes listed in the array.

var book = {              "title": "JavaScript",              "author": [                       "Bob"              ],              edition:3,            year:2001          };          var jsontext = json.stringify (book, ["title", "edition"]);          The strings saved in JSON are: Title,edition and their property values.

If the second argument is a function, the passed-in function accepts two parameters, the property name, and the property value. If the function returns undefined, then the corresponding property is ignored. (You can delete a property by returning undefined).

var book = {              "title": "JavaScript",              "author": [                       "Bob"              ],              edition:3,            year:2001          };          var jsontext = json.stringify (book, Function (key, value) {              switch (key) {case                  "author":                    return Value:join (",")                  Case ' year ':                    return;                Case "Edition":                  return undefined;                Default:                  return value;              }          });          The return value is the title, author, year, and attribute value.


2. String indentation
The third parameter of JSON controls the indentation and whitespace characters in the result, and the indent character can be any character such as a short draw line. The indent character is up to 10 characters in length.
3, ToJSON () method: Meet the needs of custom serialization. In this case, you can define the Tojson () method for the object, returning its own JSON data format.

var book = {              "title": "JavaScript",              "author": [                       "Bob"              ],              Edition:3,                        year:2001,                        ToJSON: function () {                                return this.title;                       }          };                var jsontext = json.stringify (book);

Suppose you pass an object into Json.stringify () and serialize the object in the following order:
-If there is a Tojson () method and can get a valid value through him, call the method, otherwise, return to the Shanghai object itself.
--If the second argument is provided, apply the function filter. The value of the filter passed in to the function is the value returned in the first step.
--The value returned in the second step is serialized accordingly.
--If a third argument is provided, the corresponding format is executed.

Iii. resolution Options
The Json.parse () method can also receive another parameter, which is a function that will be called on each key-value pair. This function is called a restore function. The function receives two parameters, a key and a value, and returns a value. If the Restore function returns undefined, the corresponding key is removed from the result and the other value is returned, and the value is inserted into the result.

The application of JSON in JavaScript and its own understanding

Related Article

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.