Mutual transformation of JSON and JSON strings in JavaScript

Source: Internet
Author: User

One, JSON string converted to JSON object

var str = ' {' name ': ' cxh ', ' sex ': ' Man ', ' age ': ' + '} '/ ' method one: eval-mode-parse var obj1 = eval (' (' + str + ') '); Console.log (obj1.name); // method Two: Using the global JSON object parsing, using Json.parse strict adherence to the JSON specification, such as attributes are required in quotation marks var obj2 = json.parse (str); Console.log (obj2.sex); // method Three: New function form parsing var obj3 = (new Function ("return" + str)); Console.log (obj3.age);

JSON object converted to JSON string, using global JSON object

var obj = {"Name": "Cxh", "Sex": "Man", "Age": "+"}; var str = json.stringify (obj); Console.log (str);

Mutual transformation of JSON and JSON strings in JavaScript

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.