The method of converting between Jquery string and json

The first way:

Use the js function eval();

testJson=eval(testJson); is the wrong way to convert.

The correct conversion method needs to add (): testJson = eval("(" + testJson + ")");

eval() is very fast, but it can compile and execute any javaScript program, so there will be security issues. is using eval(). The source must be trustworthy. Need to use a more secure json parser. Input that is not strictly encoded in json or if not strictly validated on the server, it is possible to provide invalid json or contain dangerous scripts, execute scripts in eval(), and release malicious code.

js code:

 

 

The second way uses the jquery.parseJSON() method, which has higher requirements on the format of json and must conform to the json format

jquery.parseJSON()

js: code


================

 . JSON.stringify(obj) : Convert a JSON object to a string

1
2
3
var  obj = [{ "href" : "baidu.com" , "text" : "test" , "orgId" :123, "dataType" : "curry" , "activeClass" : "haha" }];
 
JSON.stringify(obj);

  result:

1
"[{" href ":" baidu.com "," text ":" test "," orgId ":123," dataType ":" curry "," activeClass ":" haha "}]"

  

 .jQuery.parseJSON(jsonString) : Convert a well-formed JSON string to its corresponding JavaScript object 

1
2
3
var  str =  '[{"href":"baidu.com","text":"test","orgId":123,"dataType":"curry","activeClass":"haha"}]' ;
 
jQuery.parseJSON(str);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325751310&siteId=291194637