Conversion of JavaScript objects to JSON strings

Source: Internet
Author: User

1.eval function JSON text converted to object

In order to convert the JSON text to an object, you can use the Eval function. The Eval function calls the JavaScript editor. Because JSON is a subset of JavaScript, the compiler will parse the text correctly and produce an object structure. The text must be enclosed in parentheses to avoid grammatical ambiguity in JavaScript. var obj = eval (' (' + jsontest + ') ');    The Eval function is very fast. It can compile and execute any JavaScript program, resulting in security issues. You can use the Eval function when you are using trusted and well-developed source code. This allows for more secure parsing of JSON text. With XMLHTTP Web apps, communication between pages allows only the same origin and therefore can be trusted. But it's not perfect. If the server does not have a rigorous JSON encoding, or if there is no strict input validation, it is possible to transfer invalid JSON text that includes a dangerous script. The Eval function executes a malicious script.
View Code

2.JSON interpreter Json.parse, json.stringify

Using the JSON parser prevents the security implications of converting JSON text to objects like the Eval function. The JSON parser only recognizes JSON text and rejects all scripts. The JSON parser for a browser that provides native JSON support will be much faster than the Eval function. Currently, Firefox, Opera, IE8 and above also provide native JSON support. Among them, the functions provided by the JSON interpreter are: Json.parse, json.stringify.    for browsers that do not provide native JSON support, script json2.js can be introduced to implement the JSON conversion function. The Json2.js script can be downloaded to the Https://github.com/douglascrockford/JSON-js/blob/master/json2.js page.
View Code

3.json.parse function

Converts the JSON text to an object. Json.parse (text[, reviver]) parameter text   required option. The JSON text to convert to an object. Reviver   options available. This parameter is a replacement function. In the transformation, each node that is traversed executes the function, and the return value of the function overrides the corresponding node value of the conversion result.
View Code

4.json.stringify function

Converts an object to JSON text. Json.stringify (value[, replacer[, space]) parameter text must be selected. The object to convert to JSON text. Reviver   options available. This parameter is a replacement function. In the transformation, each node that is traversed executes the function, and the return value of the function overrides the corresponding node value of the conversion result. Space   options available. The number of spaces formatted to indent the output JSON text. If you do not provide this parameter, the output will not be formatted.
View Code

5. Delegate type for parameter Reviver

The This in the Reviver (key, value) Reviver function is the parent node of the node that is currently being traversed. When the root node is traversed, the parent node is an object, and the root node is a property of the object, and the property name is an empty string. The parameter key   is the object property name when the parent node is an array Object,key index.   value node values. Note: JSON does not support circular data structures.
View Code

6.json.parse, Json.stringify use example

<Scripttype= "Text/javascript">functionInventoryitem (parm) { This. Product=Parm. Product This. Quantity=Parm.   Quantity;  This. Price=Parm.   Price;  This. Type=Parm.   Type;  This. Total= function() {      return  This. Price*  This.   Quantity; }}functionInventory (parm) { This. Date=Parm.   Date;  This. Item=Parm.   Item;  This. Type=Parm.   Type;  This. Total= function() {      varCount= 0;  for (varKeyinch  This. Item) {Count+=  This. Item[key].      Total (); }      returncount; }}varInventoryjsontext= "{\ "date\": \ "2000-01-01\", \ "item\": [{\ "product\": \ "productone\", \ "quantity\": \ "10\", \ "price\": \ "10\", \ "Type \ ": \" inventoryitem\ "},{\" product\ ": \" producttwo\ ", \" quantity\ ": \" 100\ ", \" price\ ": \" 20\ ", \" type\ ": \" Inventoryitem\ "}],\" type\ ": \" Inventory\ "}";//converts the JSON text to an object and changes an instance of a generic class to an instance of a pseudo class. varInventoryobject=Json.parse (Inventoryjsontext,function(key, value) {vartype; if(Value&& typeofvalue=== 'Object') {type=value.      Type; if (typeoftype=== 'string' && typeofWindow[type]=== 'function') {         return New(Window[type]) (value); }   }   returnvalue;});//outputs the information of the converted object. varOutput= "product\t\tquantity\tprice\ttotal\n"; for (varKeyinchinventoryobject.item) {varItem=Inventoryobject.item[key]; Output+=item. Product+ "\ t" +item. Quantity+ "\t\t" +item. Price+ "\ t" +item. Total ()+ "\ n";} Output+= "\t\t\t\t\t" +inventoryobject.total (); alert (output);//the Inventoryobject object is then converted to JSON text. varInventoryjsontextagain=json.stringify (Inventoryobject,NULL, 3); alert (inventoryjsontextagain);</Script>
View Code

In addition, recommended to see: http://my.oschina.net/chape/blog/157332

Conversion of JavaScript objects to JSON strings

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.