The JSON.parse () and the JSON.stringify () and eval () methods use Detailed

And when the rear end of the interface, encountered a problem

Which is the time series data variables splicing, data which are all numeric data type int

But because some of the requirements need to get him some data before the data are empty

I tried to empty strings and undefined and null, nan will not work

Later, I asked a great God, and he helped me told me that after reading the string parsing json need to use eval, the method can not be used JSON.parse

I searched for relevant methods from the Internet, we were together for reference Reference

ps: when there is a mosaic escape symbol quite useless, \ this when you need multiple nested double quotation marks can be "\" "this way

Source: https://www.php.cn/js-tutorial-394185.html

 

 

"JSON ( JavaScript  Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript, because the use of language-independent text format, also used language similar to C family habits, with these characteristics make JSON data exchange over the language function is easy to read and write, but also easy to generate and parse the machine (generally used to improve network transmission rate). "  

Here today I would like to briefly talk about jquery inside JSON.parse () and JSON.stringify () function, the way will be the way native JS inside the eval () function

(1) JSON.parse function

Effects: JavaScript Object Notation (JSON)  string into an object.  

Syntax: JSON.parse (text [, reviver])

parameter:

text required. A valid JSON string.

reviver optional. A result of the conversion function. This function is called for each member object.
Returns: an object or array

example:

 

1

2

3

var json = '{"name":"GDT","age":,"University":"GDUT"}';

var info = JSON.parse(json);  //解析为JSON对象

document.write(info.name + ' is a student of ' + info.University + ' and he is ' + info.age + " years old."); /info为Object对象

(2) JSON.stringify () function

Effects: JavaScript value into JavaScript Object Notation (JSON) string

语法:JSON.stringify( value [, replacer] [, space])

parameter:

Required value, typically JavaScript value to be converted (typically an object or array)

Alternatively replacer, or a function for converting the result array

space optional. Adding JSON return value to indent text, spaces and line breaks to make it easier to read.

Return Value: a string containing the text JSON

example:

1

2

3

var info = {name:"GDT",age:,University:"GDUT"};

var json = JSON.stringify(info); //转换为JSON字符串

document.write(json); //output为{"name":"GDT","age":23,"University":"GDUT"}

(3) eval () function

Action: eval () function computes a string, and executes the JavaScript code.

Syntax: eval (string)

parameter:

Required string, the string to be calculated, to calculate the JavaScript containing  an expression statement or to be executed.

Returns: the calculated value of the string, if any, (not then return without any change)

example:

1

2

3

4

eval("x=;y=;document.write(x*y)"); //output为

document.write(eval("+"));  //output为

var x=;

document.write(eval(x+));  //output为

Focus here:

 Use eval () function may be resolved to an object JSON string, this function can complete the JSON.parse () function , but not the same place, consider the following Code

1

2

3

4

5

6

7

8

// JSON.parse()

var json = '{"name":"GDT","age":,"University":"GDUT"}';

var info = JSON.parse(json);    //解析为JSON对象

document.write(info); //output为[object Object]

//eval()

var json = '{"name":"GDT","age":,"University":"GDUT"}';

var info = eval('(' + json + ')'); //解析为JSON对象

document.write(info); //output为[object Object]

I do not know if you have noticed wood eval () but also with a pair of parentheses string wrap, which I find a better explanation is:

Reason: eval itself due to the problem, since by way json "{}" to the beginning and the end, in JS, it will be processed as a block of statements, it is mandatory to convert it into an expression formula.

Solution: with parentheses purpose is to force the eval function to force the expression (expression) in parentheses when processing JavaScript code conversion for the object, rather than as a statement (statement) to execute. As an example, such as object literal {}, if it does not increase the outer brackets, braces will then eval start and end tags identified as JavaScript code block, then the {} will be considered to be an empty statement executed.

Consider the following example of a different

1

2

alert(eval("{}")); // return undefined

alert(eval('('+'{}'+')')); // return object[Object]

  In addition, with respect to the written format stringent JSON.parse () is, eval () can parse any string, eval is unsafe because eval more relaxed, there will be a potential security problem . For example, the following code:

1

2

3

4

var str = '{"a":"b"}';

document.write(eval("("+str+")")); //正常解析为对象

var str = '{"a": (function(){alert("I can do something bad!");})()}';

eval('('+str+')'); //可以用来执行木马脚本

  If a malicious user to inject a string in json script insertion Trojans link to a page, you can also use eval operation, but with JSON.parse () you do not have to worry about this problem, visible, although the eval () function is very powerful, but the actual opportunity to use not many.

Guess you like

Origin www.cnblogs.com/huchong-bk/p/11459017.html