JSON in Javascript

Source: Internet
Author: User

JSON in Javascript

Javascript is a target-oriented Page scripting language that runs on a Web browser. Many people think it is a subset of Java, but it is not. It is a solution-a language that is similar to the C language syntax and has a weak type. Javascript is standardized in ecmascript language specification and third edition.

JSON is a subset of the original semantic tag of the script target. Because JSON is a subset of scripts, you can use it without a doubt.

VaR myjsonobject = {"bindings ":[

{"Ircevent": "PRIVMSG", "method": "newuri", "RegEx": "^ http ://.*"},

{"Ircevent": "PRIVMSG", "method": "deleteuri", "RegEx": "^ Delete .*"},

{"Ircevent": "PRIVMSG", "method": "randomuri", "RegEx": "^ random .*"}

]

};

In this example, the program creates an object with only one "bindings" member. This "bindings" member has an array containing three objects, each of which contains "ircevent", "method", "RegEx" members.

Members can be obtained by using small dots or subscripts. For example, you can use myjsonobject. bindings [0]. method to obtain the inverted "newuri" member.

Myjsonobject. bindings [0]. Method // "newuri"

You can use the eval () function to convert a JSON text into an object. The eval () function calls the script compiler. Because JSON is an exclusive subset of scripts, the compiler will parse the text correctly and create a class structure.

VaR myobject = eval ('+ myjsontext + ')');

The eval function is very fast. However, it can compile and execute any script program, so it may cause security problems. The eval function can be used only when the source code is trusted and valid. When the network server provides both the base page and JSON data, this is very common in network applications. Sometimes the source code is untrusted. In fact, the client is never trusted.

It is better to use JSON parsing when security is important. JSON parsing only recognizes JSON text and is more secure. JSON is as follows:

VaR myobject = JSON. parse (myjsontext, filter );

The optional filter parameter is a function. This function will be called by every key and value at each layer of the final result. Each value is replaced by the result of the filter function. This can be used to convert a common object to an instance of a specific object. As follows:

Mydata = JSON. parse (text, function (Key, value ){
Return key. indexof ('date')> = 0? New Date (value): value;
});

JSON stringifier is in the opposite direction, which converts script data to JSON text. JSON does not support the cyclic data structure. Therefore, do not input the cyclic structure into JSON stringifier.

VaR myjsontext = JSON. stringify (myobject );

If the stringify method encounters an object containing the tojson method, it will call this method and return this value. In this way, an object is allowed to define its own JSON proxy.

The stringifier method can receive an optional character array. These strings are used to select attributes contained in JSON text. However, all attributes of an object will be included. Under any circumstances, values in JSON will be excluded (such as functions and undefined values) If no proxy is available ).

The open source code of a JSON parser and JSON stringifier is available. When minified it is less than 2 K.

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.