Js Operation JSON

Source: Internet
Author: User

JSON is a built-in object that provides the stringify and parse methods used to convert a JS object into a JSON-compliant string, which converts a JSON-compliant string into a JS object. The parse method is equivalent to the eval () method  var obj =json.parse (str); = var obj = eval (' (' + str + ') '); Note: If obj is originally a JSON object, then the use of the Eval () function after conversion (even if multiple conversions) is a JSON object, but the use of the Parsejson () function is problematic (throwing a syntax exception).   Json.parse () can only handle strings in JSON form, otherwise error, while Eval () is the content of the execution string;   Case One: (JSON is Object)var json={' a ': ' AA ', ' B ': ' BB ', ' C ': ' CC '};var str=json.srtingjfy (JSON);var obj=json.parse (str);  Console.log (JSON);Console.log (typeof str+ "" +str);console.log (obj); Results:   Case Two: (JSON is a string)var json= '{ ' name ': ' cxh ', ' sex ': ' Man '} '; //Note: The outermost is enclosed in single quotes with double quotes inside, otherwise an error var obj=json.parse (JSON); var str=json.srtingjfy (str); Console.log (typeof json);console.log (obj);Console.log (typeof str+ "" +str); Results:

Js Operation JSON

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.