Introduction to the notes in the jquery eval parsing json

Source: Internet
Author: User

In JS to parse the JSON string into JSON data format, generally there are two ways: using the eval () function, using the Function object for return parsing, here is an example, interested friends can refer to the following

In js, the JSON string is parsed into a JSON data format, typically in two ways:

1. One for using the eval () function.

2. Use the Function object for return Parsing.

Use the Eval function to parse, and use the each method of jquery to traverse

Using jquery to parse JSON data as a transport object for jquery asynchronous requests, The result of the jquery request is the JSON object, which is all about the server returning the form of a string in JSON form. For JSON objects that are encapsulated with plug-ins such as jsonobject, This is also the same thing, and is no longer explained Here.
Here we first give a set of JSON strings, the string set as Follows:

The code is as Follows:

var data= " {root: [{name: ' 1 ', value: ' 0 '}, {name: ' 6101 ', value: ' Beijing '}, {name: ' 6102 ', value: ' Tianjin '}, {name: ' 6103 ', value: ' Shanghai city '}, {name: ' 6104 ', value: ' Chongqing '}, {name: ' 6105 ', value: ' Weinan '}, {name: ' 6106 ', value: ' Yanan '}, {name: ' 6107 ', Value: ' Hanzhong '}, {name: ' 6108 ', value: ' Yulin '}, {name: ' 6109 ', value: ' Ankang '}, {name: ' 6110 ', value: ' Shangluo '}]}

This is based on the data type obtained by jquery Asynchronously,--json object and string, and describes how the results are handled in two ways. &NBSP

1. For the JSON string returned by the server, if the jquery asynchronous request did not make a type description, Or as a string to accept, then need to do an object processing, the way is not too cumbersome, is to put the string in eval () to execute once (if the request type is json, the server put back is also json, no eval ())   This is also a good way to get JSON objects in a normal Javascipt way, as illustrated by the following example:  

var Dataobj=eval ("(" +data+ ")");//convert to JSON object  
Why do you want to add "(" ("+data+") to the eval here?//"?  

Because of the problem with eval itself. Since JSON starts and ends in the form of "{}", 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 the expression in parentheses to be converted to an object while processing the JavaScript code, rather than being executed as a statement (statement). For example, if the object literal {} is not enclosed, then Eval will recognize the curly brace as the start and end tag of the JavaScript block, and {} will be considered an empty Statement. So the following two execution results are different:  

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

For this kind of writing, in js, you can see Everywhere.

Such as: (function ()) {} (); Do the closure operation and so On.

Alert (dataObj.root.length); //    If (idx==0 return true/ /  Alert ("name:" +item.name+ ", value:" +

Note: for a generic js-generated json object, you only need to replace the $.each () method with the for statement, and the other is Unchanged.  

2. For the JSON string returned by the server, if the jquery asynchronous request sets the type (typically this configuration Property) to "json" or the $.getjson () method to get the server back, then the eval () method is not Required. Because the result is already a JSON object, just call the object directly, here the $.getjson method as an example to illustrate the method of data processing:

 $.getjson ("http://www.phpzixue.cn/", {param: "gaoyusi"},function   //  The data returned here is already a JSON object  Span style= "color: #008000;" >//  $.each (data.root,  (idx,item) { if  (idx==0< Span style= "color: #000000;" ) { return  true ; //  Same as countinue, returns false with break   "alert (  name: "+item.name+", value: "+item.value");});  

In particular, it is important to note that the Eval () method in mode 1 is the dynamic execution of strings (possibly JS scripts), which can easily cause system security Problems. So you can use some Third-party client script libraries that circumvent Eval (), such as JSON in javascript, which provides a script library of no more than 3k.

The second parsing method is to use the function object to complete, its typical application is in jquery in the Ajax method of success and so on the return data of the parsing

var json= ' {"name": "CJ", "age": +} '= (new

The data at this point is the one that will parse into a JSON object.

Introduction to the notes in the jquery eval parsing json

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.