Example Method for JavaScript to Parse JSON Format Data

  • 2021-07-15 03:41:34
  • OfStack

In this paper, an example is given to explain the method of JavaScript parsing JSON format data. Share it for your reference, as follows:

1. Use the eval () function provided by JavaScript


function JsonText1() {
  var strJSON = "{'Name':'Kevin','Age':'23'}"; // Get JSON
  var obj = eval("(" + strJSON + ")"); // After conversion JSON Object 
  alert(obj.Name);
}

2. Use the JSON object

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

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

Use the parse () function of the JSON object to convert JSON into an object

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


var jsonStr = "";
// Use JSON Object's stringify() Function to convert an object to a JSON
function JsonText2() {
  var data = new Object;
  data.Name = "Kevin";
  data.Age = 23;
  jsonStr = JSON.stringify(data);
  alert(jsonStr);
}
// Use JSON Object's parse() Function to set the JSON Convert to an object 
function JsonText3() {
  var data = JSON.parse(jsonStr);
  alert(data.Name);
}

PS: Regarding json operation, here we recommend several practical json online tools for your reference:

Online JSON code verification, verification, beautification and formatting tools:
http://tools.ofstack.com/code/json

JSON Online Formatting Tool:
http://tools.ofstack.com/code/jsonformat

Online XML/JSON interconversion tool:
http://tools.ofstack.com/code/xmljson

json code online formatting/beautification/compression/editing/conversion tool:
http://tools.ofstack.com/code/jsoncodeformat

Online json compression/escape tool:
http://tools.ofstack.com/code/json_yasuo_trans

C Language Style/HTML/CSS/json Code Formatting and Beautification Tool:
http://tools.ofstack.com/code/ccode_html_css_json

For more information about JavaScript, please see the topics of this site: "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: