Json.parse () and Eval ()

Source: Internet
Author: User

First, preface

as with XML, JSON is also a data format based on plain text. Since JSON is inherently prepared for JavaScript, the JSON data format is very simple, you can transfer a simple string,number,boolean with JSON, or you can transfer an array, or a complex object.

Second, a string containing a JSON object

basic formula: var jsondata= ' { "data1": "Hello,", "data2": " world! "

Three, the analytic method of JSON

(1) use Json.parse ()

<script>
var str = ' {' name ': ' Party ', ' age ': ' 23 '} ';
var ob=json.parse (str);
alert (ob.name);
alert (ob.age);

var str = ' [' Name ', ' age '] ';
var ob=json.parse (str);
Alert (ob[0]);
Alert (ob[1]);
</script>

(2) Use eval ()

var str = ' {' name ': ' Party ', ' age ': ' 23 '} ';
var ob=eval("("+str+")");

alert (ob.name);
alert (ob.age);

Note: Json.parse () can parse data in JSON format, and will format the string to be parsed, not parse if it is malformed, and eval () can parse any string, especially third-party JSON data (which may contain malicious code). Using eval is dangerous and prone to memory leaks, so use the Json.parse () method to parse strings in your code whenever possible.

Json.parse () and Eval ()

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.