The conversion of object to string in JS

Source: Internet
Author: User

When using JSON2. JS file Json.parse (data) method, encountered a problem:

throw new SyntaxError (' Json.parse ');

The Json.parse method throws an SyntaxError exception when it encounters a non-resolvable string

Json.parse (text, Reviver) This method parses a JSON text to produce an object or array. It can throw a SyntaxError exception. The optional reviver parameter is a function that can filter andtransform the results. It receives each of the keys and values,and their return value is used instead of the original value. If it returns what it received and then the structure was not modified. If it returns undefined then the member is deleted.text required. A valid JSON string. Reviver is optional. A function that transforms the result. This function is called for each member of the object. If the member contains nested objects, the nested objects are converted before the parent object. For each member, the following happens: If Reviver returns a valid value, the member value is replaced with the converted value. If Reviver returns the same value it receives, the member value is not modified. If reviver returns null or undefined, the member is deleted.

One, jquery plug-in support for the conversion mode

var stra = ' {' FirstName ': "Chen", "surname": "Pingzhao", "Phone": ["010-12345678", "010-23456789"]} ';  $.parsejson (Stra); Jquery.parsejson (stra); var fields = $ ("SELECT,: Radio"). Serializearray ();

Second, browser-supported conversion mode (FIREFOX,CHROME,OPERA,SAFARI,IE9,IE8) and other browsers

Third, JavaScript support conversion mode

Iv. JSON official Conversion mode


http://www.json.org/, provides a json.js so that IE8 (compatibility mode), IE7 and IE6 can support JSON objects as well as their stringify () and Parse () methods;

The sample code is as follows:

<script type= "Text/javascript" >//Example 1: This example uses Json.parse to convert a JSON string      For the object var stra = ' {"FirstName": "Chen", "surname": "Pingzhao", "Phone": ["010-12345678", "010-23456789"]} ';      var stra = Json.parse (stra);      document.write (Stra.surname + "," + Stra.firstname + "," + Stra.phone); Example 2: This example uses Json.parse to convert a JSON string to an object calling the callback function var StrB = ' {"Teststart": "1990-01-01t12:00:00z", "Testend": "2015-12-2      5t12:00:00z "}";      var StrB = Json.parse (StrB, datereviver);        document.write ("<br/><br/>" +strb.teststart.toutcstring ());          function Datereviver (key, value) {var A; if (typeof value = = = ' String ') {a =/^ (\d{4})-(\d{2})-(\d{2}) T (\d{2}):(\d{2}):(\d{2} (?: \. \d*)?)              Z$/.exec (value);              if (a) {return new Date (DATE.UTC (+a[1], +a[2]-1, +a[3], +a[4],+a[5], +a[6]));      }} return value;  }; </script> 

The format of the JSON string must be standard, key and value must be enclosed in double quotation marks, otherwise the line parsing exception


The conversion of object to string in JS

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.