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