JavaScript Advanced Programming Notes (20)

Source: Internet
Author: User

Json

JSON is a strict subset of JavaScript that leverages some of the patterns in JavaScript to represent structured data. JSON is a better way to read and write structured data in JavaScript. Because you can pass the JSON to eval (), you don't have to create a DOM object.

(a) grammar

Simple values: string, numeric, array, and null, undefined are not supported.

Object: A set of unordered key-value pairs, which can be simple values or values of complex data types.

Array: A list of ordered values that can be accessed by a numeric index, and the value of an array can be any type.

1. Simple values

String must use double quotation marks

2. Objects
{      "name" = "Nicolas",      "age" = 29,
"School": {
"Name": "Merrimack College",
"Location": "North"
}}

No variable declared

No semicolon at the end

The property value of the object must be double-quoted

Property values can be simple values, which can be complex type values

3. Arrays

No variables and semicolons

(ii) parsing and serialization

You can parse the JSON data structure into useful JavaScript objects.

1.JSON objects

Support: ie8+ firefox3.5+ Safari 4+ Chrome Opera 10.5+ Earlier versions can use a shim.

Stringify () Serializes the JavaScript object into a JSON string, without any space characters and indentation.

The parse method parses the JSON string into the native JavaScript value.

2. Serialization operations

Json.stringify () can also accept two parameters, the first parameter is a filter, it can be an array or a function, and the second parameter indicates whether the JSON string retains indentation.

① Filter Results

If the filter parameter is an array, the result retains only the attributes in the array.

If the second argument is a function, the incoming function receives two parameters, the property name, and the property value. If the function returns undefined, the corresponding property is ignored.

        varJsontext = json.stringify (book,function(key, value) {Switch(key) { Case"Authors":                    returnValue.join (",")                                  Case"Year":                    return5000;  Case"edition":                    returnundefined; default:                    returnvalue; }        });

② String Indentation

The third parameter controls the indentation and whitespace characters, and if it is a number, the number of spaces that are indented for each level, and all values greater than 10 are converted to 10.

var null, 4);

If the incoming value is not numeric, the string will be converted to 10 if the JSON string is used as an indent string, and the number of characters greater than 10 can be set to a tab or two dashes.

③tojson method

Converts a JavaScript data object to an ISO 8601 date string.

        var book = {                       "title": "Professional JavaScript",                        "authors": [                            " Nicholas C. Zakas "                        ],                        3,                        ()                        ,function() {                             return  This . title;                        }                   };

If there is a Tojson () method that can be used to obtain a valid value, the method is called, otherwise the object itself is returned.

If the second parameter is provided, apply the filter. The value passed into the function filter 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 formatting is performed.

3. Resolution Options

Json.parse () can accept another parameter, which is a function that is called on a key-value pair. If the Restore function returns undefined, it means that the corresponding key is removed from the result, and if another value is returned, Zell inserts the value into the result. A date string is often used when it is converted to a DateTime object.

        var function (key, value) {            if (key = = "ReleaseDate")                {return  undefined            ; Else {                return  value;            }        });

JavaScript Advanced Programming Notes (20)

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.