jquery methods for parsing data in JSON format (object, String)

Source: Internet
Author: User

Related functions
function Description
Json.parse () Used to convert a JSON string to a JavaScript object.
Json.stringify () Used to convert JavaScript values to JSON strings.
Data is a string type to convert the string type to the JSON data type var jsondatas=eval ("(" +data+ ")");



The example in this paper describes how jquery parses JSON-formatted data. Share to everyone for your reference, as follows: JSON data is a small kind of data we commonly used in real-time exchange of a thing, he can use jquery or JS to parse, let me introduce the jquery parsing JSON string method. I. jquery parsing JSON data format: Using this method, you must set the parameters in the AJAX request: DataType:"JSON"get the data returned by the callback function and parse to get the value we want, see the source code:?Jquery.ajax ({url:full_url, DataType:"JSON", Success:function(results) {alert (result.name);}}); Typically, you can return JSON data from the background, and the front desk will hand it to jquery, haha!! The jquery asynchronous request sets the type (typically this configuration property) to "JSON", or the $.getjson () method to get the server back, so the eval () method is not needed because the result is already a JSON object, just call the object directly, Here is an example of the $.getjson Method 1 code as follows:?varData= "{root: [{name: ' 1 ', Value: ' 0 '}, {name: ' 6101 ', Value: ' Beijing '}, {name: ' 6102 ', Value: ' Tianjin '}, {name: ' 6103 ', Value: ' Shanghai '}, {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 '}]}"; jquery?$.getjson ("http://www.jb51.net/", {param: "Sanic"},function(data) {//The data returned here is already a JSON object//The following other actions are the same as the first case$.each (Data.root,function(idx,item) {if(idx==0){ return true;//same as Countinue, return false with break} alert ("Name:" +item.name+ ", Value:" +item.value); }); }); Ii. jquery parsing JSON objects: jquery provides another way to "Parsejson", which requires a standard JSON string and returns the generated JavaScript object. Let's take a look at the syntax: Copy the code code as follows: Data=$.parsejson (string); see how it's applied to real-world development:?Jquery.ajax ({url:dataurl, success:function(results) {varParsedjson =Jquery.parsejson (results); alert (parsedjson.name);} }); add: jquery Parses a complete example of JSON data:?varData= "{root: [{name: ' 1 ', Value: ' 0 '}, {name: ' 6101 ', Value: ' Beijing '}, {name: ' 6102 ', Value: ' Tianjin '}, {name: ' 6103 ', Value: ' Shanghai '}, {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 '}]}"; //data is a string type to convert the string type to a JSON data typevarJsondatas=eval ("(" +data+ ")"); $.each (Jsondatas.root,function(i,n) {alert ("Name" +n.name+ "value" +n.value);})//the following array type string is converted to JSON string parsing//JSON string in array form varJsondata= "[{name: ' 1 ', Value: ' 0 '}, {name: ' 6101 ', Value: ' Xian City '}, {name: ' 6102 ', Value: ' Tongchuan '}, {name: ' 6103 ', Value: ' Baoji '}, {name: ' 6104 ', Value: ' Xianyang '}, {name: ' 6105 ', Value: ' Weinan '}, {name: ' 6106 ', Value: ' Yanan '}, {name: ' 6107 ', Value: ' Hanzhong '}, {name: ' 6108 ', Value: ' Yulin '}, {name: ' 6109 ', Value: ' Ankang '}, {name: ' 6110 ', Value: ' Shangluo '}] ";varjson=eval (jsondata); $.each (JSON,function(i,n) {alert (json[i].name); alert (json[i].value);//value based on index});//JSON data characters do not need to be convertedvarjson={"Products":[ {"OrderID": "11077", "CustomerID": "RATTC"}, {"OrderID": "11078", "CustomerID": "Ratt"} ], "IMG": [{"id": "12345", "url":"Image/1.jpg"} ]}; $.each (JSON. Products,function(i,n) {alert (N.orderid);}) General processing files (HANDLER.ASHX)?if(Context. Request.querystring["Method"]! =NULL) {string method= Context. Request.querystring["Method"]. ToString (); if(method = = "GetList")) {string str= configurationmanager.connectionstrings["ConnectionString"]. ConnectionString; SqlConnection Conn=NewSqlConnection (str); Conn. Open (); SqlCommand cmd=NewSqlCommand (); Cmd. Connection=Conn; Cmd.commandtext= "Select Proid,proname,url from Project where Adress = ' Harbin '"; DataSet DS=NewDataSet (); SqlDataAdapter da=NewSqlDataAdapter (CMD); Da. Fill (DS); String SB= Createjsonparameters (ds. Tables[0]); Context. Response.clearcontent (); Context. Response.Write (sb.) ToString ()); Context. Response.End (); } } } ///<summary> ///Build JSON string ///</summary> ///<param name= "DT" ></param> ///<returns></returns>Public string createjsonparameters (DataTable dt) {System.Text.StringBuilder SB=NewSystem.Text.StringBuilder (); if(dt! =NULL&& dt. Rows.Count > 0) {sb. Append ("["); for(inti = 0; i < dt. Rows.Count; i++) {sb. Append ("{"); for(intj = 0; J < dt. Columns.count; J + +) { //If the value is not the last one, add a comma-delimited if(J < dt. Columns.count-1) {sb. Append ("/" "+ dt. COLUMNS[J]. Columnname.tostring () + "/": "+"/"" + dt. ROWS[I][J]. ToString () + "/", "); }//If the value is the last character, the comma else if (j = = DT) is not added. columns.count-1) {sb. Append ("/" "+ dt. COLUMNS[J]. Columnname.tostring () + "/": "+"/"" + dt. ROWS[I][J]. ToString () + "/" "); } } //do not add a comma if it is the last value if(i = = dt. Rows.count-1) {sb. Append ("}"); } Else{sb. Append ("},"); }} sb. Append ("]"); returnsb. ToString (); } Else{return NULL; } }?$.ajax ({type:"POST", URL:"Handler.ashx?method=getlist", Async:false,//true to indicate that asynchronous false indicates synchronizationContentType: "Application/json", DataType:' JSON ', Success:function(Result) {vartemp=eval (result); //parsing the returned array string through JavaScript for(vari = 0; i < temp.length; i++) {o.innerhtml+ = "Project name:" + result[i]. Proname + "<br/> URL: <a href=" + result[i].url + "mce_href=" + Result[i].url + "target= ' _blank ' >" + result[i]. URL + "</a><br/>"; Datas+ = "Project name:" + result[i]. Proname + "<br/> URL: <a href=" + result[i].url + "mce_href=" + Result[i].url + "target= ' _blank ' >" + result[i]. URL + "</a><br/>"; } TINY.box.show (Datas,0, 0, 0, 1); }});

jquery methods for parsing data in JSON format (object, String)

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.