JS Operation JSON Detailed summary

Source: Internet
Author: User
Tags eval json opening and closing tags

This article is mainly on the JS operation JSON summarized introduction, the need for friends can come to the reference, I hope to help you

There are generally two ways to parse a JSON string into a JSON data format in JS:

1. One is to use the Eval_r () function.

2. Use function objects to perform return parsing.

In the data transfer process, JSON is passed in the form of text, a string, and JS is the JSON object, so the conversion between JSON and JSON strings is critical. For example:

JSON string:

var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';

JSON object:

var str2 = {"Name": "Cxh", "Sex": "Man"};

The first workaround:

var dataobj=eval_r ("(" +data+ ")");//Convert to JSON object

Why do I have to eval here to add "(" ("+data+"));/"?

The reason is that eval itself is a problem. Since JSON starts and ends with "{}", in JS it is treated as a block of statements, so it must be coerced into an expression.

The purpose of parentheses is to force the Eval function to force an expression in parentheses (expression) into an object when processing JavaScript code instead of executing as a statement (statement). For example, an object literal {}, if the outer bracket is not added, Eval recognizes the curly braces as the opening and closing tags of the JavaScript code block, then {} will be considered to have executed an empty statement. So the following two execution results are different:

Alert (Eval_r ("{}");//return undefined

Alert (Eval_r ("({})");//return Object[object]

For this type of writing, in JS, you can see everywhere.

such as: (function ()) {} (); When doing closure operation.

var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';

var data=eval_r ("(" +str1+) ");//Convert to JSON object//data = (new

alert (data.name);//will show cxh

In particular, it is important to note that the Eval_r () method in mode 1 dynamically executes the string (possibly a JS script), which can easily cause security problems in the system. So you can use some Third-party client script libraries that circumvent eval_r (), such as JSON in JavaScript, which provides a script library of no more than 3k.

The second workaround:

The second way to do this is to use a function object to do it, and it's typical application is the success of the Ajax method in jquery to the return data

var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';

var data = (Function ("", "Return" +STR1)) ();

alert (data.name);//will show cxh

Detailed Source reference: http://www.jb51.net/article/47401.htm

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.