Home > Article > Web Front-end > Implement json conversion of web service based on jQuery's ajax function_jquery
But I really don’t know how to come up with the title of this article. It would be a pity if you missed this thing because of this poor title.
I referred to many articles before making this thing:
http://www.roseindia.net/tutorials/json/parse-message-JSON-JS.shtml
http://funkatron.com/site/comments/safely-parsing-json-in-javascript/
http://docs.jquery.com/Ajax/jQuery.getJSON
http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/
http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/
The first step you need to do is how to convert a datatable to the format you need on the server side:
Originally I wanted to use .net's javascriptSerializer to complete it (combined with Genirics library's List, etc.), but later I found that using NewtonSoft.Json is more flexible and has better scalability. I even found some codes on the Internet that can be used ready-made.
Let’s take a look at how I implemented Data conversion on the server side:
Not only Datatable but also DataSet can be converted here, and the conversion function of JavascriptSerializer will be expanded in the future. It should be noted that the final result of the conversion needs to be a string wrapped in []. At first, I tried to use {} to finally return json as an object. As a result, the client really had no clue and could not convert at all. Finally, I saw an article introducing that in .net 3.5, the results returned by your web services will be automatically packaged into a json object with the name d, such as: {"d":***}. So I only have The server side generates a package in array form.
Then I created a database table with the following content:
,
The web methods defined byare as follows:
Serializer method has been packaged in DNA_JSON. It is extended based on NewtonSoft.Json's basic conversion class.
The second step is the client stuff:
First we need to use a few js, jQuery.js, json2.js is OK.
As shown in the picture above, we use jQuery’s ajax and JSON. Parse() conversion function.
It is also important to note that res.d is the attribute of .net 3.5’s packaged json by default. If you use framework2.0, you won't have this problem.
Finally, let’s see how to display the array composed of these objects in a table. The code is as follows: