JS Parse json instance with eval to share with considerations

Source: Internet
Author: User
Tags eval json

The

 eval function is useful in JS, so let me introduce you to an example of Eval parsing JSON, based on this example, we draw some considerations for the eval processing of JSON data, and I hope the article will be helpful to the alumni.

JSON (JavaScript Object notation) is a simple data format that is lighter than XML. JSON is the native format of JavaScript, which means that processing JSON data in JavaScript does not require any special APIs or toolkits. The rule of JSON is simple: an object is an unordered set of ' name/value pairs '. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" followed by a ":" (colon); "' Name/value ' pair" with "," (comma) separation   First look at a resolution instance   code as follows: <! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" > <html>   <head>   <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>   <title>eval function parsing JSON object </title>    </head>  <body>   <script type= "Text/javascript" >   var json=eval ("{sitename: ' Dreamdu ', Sitedate:new Date (1980, 12, 17 , 12, 0, 0)});   document.write (json.sitename);    document.write ("<br/>");    document.write ( json.sitedate);    </script>  </body> </html>     eval () The issues to be noted in parsing JSON format strings Issues to be aware of using eval () to parse a JSON format string when you convert a JSON-formatted string to a JS object using the JavaScript built-in eval function,You need to wrap the string first with a pair of "()". For example: the Var strtest= "{id:" Cnlei ", url:" Http://www.jb51.net "}"; Convert to JS object correctly: Var objtest=eval ("(" +strtest+) ");  error: Var objtest=eval (strtest);   Complete test code:     Code as follows: <script type= "Text/javascript" > <!--    var strtest= "{id:" Cnlei ", u RL: "Http://www.jb51.net"} ";     var objtest=eval ("(" +strtest+) "); Correct wording    //var objtest=eval (strtest); Error writing     alert (objtest.id+ "n" +objtest.url); --> </script>

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.