I have been working on. NET development for more than two months from scratch. During this period, I often encounter the need to transmit data from the Web Front-end to the backend for processing. For example, the following (Ajax post using jquery) is used. For the convenience of demonstration, the encodeuricomponent operation is not performed on the parameter:
$.post( "/Home/Index", { "name": "feichexia", "age": 23, "email": "feichexia@yahoo.com.cn" } function(data) { // TODO } }
Then the index action in homecontroller. CS in the ASP. NET background may be handled as follows:
public ActionResult Index(string name, int? age, string email) { // TODO }
There is nothing wrong with this, but here we want to provide a new way to pass parameters: put several parameters with relevance together and pass them to the background in the form of objects, this is more in line with the OO encapsulation philosophy.
Let's take a look at the js json operation method:
JSON (JavaScript Object Notation) is a lightweight data exchange format. It adopts a completely language-independent text format and is an ideal data exchange format. JSON is a native JavaScript format, which means that no special API or toolkit is required to process JSON data in JavaScript. This article mainly summarizes the essentials for JS operations on JSON. In JSON, there are two structures: objects and arrays. 1. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs. The name is enclosed in quotation marks. If the value is a string, it must be enclosed in parentheses, but not numeric. For example: var o = {"xlid": "cxh", "xldigitid": 123456, "topscore": 2000, "topplaytime": ""}; 2. an array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas. For example: var jsonranklist = [{"xlid": "cxh", "xldigitid": 123456, "topscore": 2000, "topplaytime "}, {"xlid": "ZD", "xldigitid": 123456, "topscore": 1500, "topplaytime": "2009-11-20"}]; To process JSON data conveniently, JSON provides JSON. JS package,: http://www.json.org/json.js in the data transmission process, JSON is transmitted in the form of text, that is, strings, and JS operations are JSON objects, so, conversion between JSON objects and JSON strings is critical. Example: JSON string: var str1 = '{"name": "cxh", "sex": "Man"}'; JSON object: var str2 = {"name ": "cxh", "sex": "Man"}; 1. to convert a JSON string to a JSON object, use str1 in the preceding format and convert it to a JSON object in the following way: // convert the JSON string to the JSON object var OBJ = eval ('+ STR +'); or var OBJ = Str. parsejson (); // converts a JSON string to a JSON object or var OBJ = JSON. parse (STR); // converts a JSON string to a JSON object and then reads: Alert (obj. name); alert (obj. sex); Pay special attention: If obj is a JSON object, then after using the eval () function conversion (If it is converted multiple times) or a JSON object, you may have questions after using the parsejson () function (throwing a syntax exception ). 2. You can use tojsonstring () or the global JSON. stringify () function to convert a JSON object to a JSON string. For example, VAR last = obj. tojsonstring (); // converts a JSON object to a JSON character or var last = JSON. stringify (OBJ); // converts a JSON object to the JSON character alert (last); Note: among the above multiple essentials, except that the eval () function is provided by JS, all other essentials come from JSON. JS package. The new JSON version modifies the API and changes JSON. stringify () and JSON. the two essentials of parse () are injected into the built-in JavaScript Object. The former becomes the object. tojsonstring (), and the latter is a string. parsejson (). If you cannot find the tojsonstring () and parsejson () essentials, it indicates that your JSON package version is too low.
So many of the most critical elements are two JSON. stringfy () and JSON. parse (). The former converts a JSON object into a JSON string (serialization), and the latter converts a JSON string into a JSON object (deserialization ).
This is simple, front-end:
var person = { "name": "feichexia", "age": 23, "email": "feichexia@yahoo.com.cn"};$.post( "/Home/Index", { "person": encodeURIComponent(JSON.stringify(person)) }, function(data) { // TODO });
ASP. NET background only needs this:
public ActionResult Index(string person) { JavaScriptSeriliazer serializer = new JavaScriptSeriliazer(); Person p = (Person) serializer.Deseriliaze(person); // TODO return View();}
If the ASP. NET background returns a JSON object string to the foreground, the foreground only needs to call JSON. parse to deserialize it into a JSON object.