Example of how JavaScript parses data in JSON format: javascriptjson

Source: Internet
Author: User

Example of how JavaScript parses data in JSON format: javascriptjson

This document describes how JavaScript parses JSON data. We will share this with you for your reference. The details are as follows:

1. Use the eval () function provided by JavaScript

Function JsonText1 () {var strJSON = "{'name': 'kevin ', 'age': '23 '}"; // JSON var obj = eval ("(" + strJSON + ")"); // converted JSON object alert (obj. name );}

2. Use a JSON object

① Use the stringify () function of the JSON object to convert the object to JSON

Syntax: var str = JSON. stringify (data );

② Convert JSON into an object using the parse () function of the JSON object

Syntax: var data = JSON. parse (str );

Var jsonStr = ""; // use the stringify () function of the JSON Object to convert the Object to JSONfunction JsonText2 () {var data = new Object; data. name = "Kevin"; data. age = 23; jsonStr = JSON. stringify (data); alert (jsonStr);} // use the parse () function of the JSON object to convert JSON into the function JsonText3 () {var data = JSON. parse (jsonStr); alert (data. name );}

PS: For json operations, we recommend several useful json online tools for your reference:

Online JSON code verification, validation, beautification, and formatting tools:
Http://tools.jb51.net/code/json

JSONOnline formatting tool:
Http://tools.jb51.net/code/jsonformat

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

JsonCode Online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat

Online json compression/escape tools:
Http://tools.jb51.net/code/json_yasuo_trans

C language style/HTML/CSS/json code formatting and beautification tools:
Http://tools.jb51.net/code/ccode_html_css_json

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.