ASP. NET MVC Web site development Summary (vi)--Brief talk on the serialization and deserialization of JSON

Source: Internet
Author: User

First, let's start with a simple discussion of what serialization and deserialization, serialization (serialization), transforms the state information of an object into a form that can be stored or transmitted. During serialization, an object writes its current state to a temporary or persistent store. Later, the object can be recreated by reading or deserializing (deserialization) the state of the object from the store.

In general, where do we use JSON serialization and deserialization? For web site development, we generally use JSON for foreground and background data transfer (often used with Ajax), and this process involves the serialization and deserialization of JSON.

So let's just take a look at JSON serialization and deserialization instances:--asynchronously loading user data into the foreground using AJAX, passing data through JSON

An object: (the user stores some information about the user, which is serialized as the corresponding JSON data when transferred)

//User section Information     Public classUserInfo { Public stringName {Get;Set; }  Public stringheadportrait {Get;Set; }  Public intGrade {Get;Set; }  Public stringMajor {Get;Set; }  Public intClass {Get;Set; }  Public stringAddress {Get;Set; }  PublicUserInfo (stringNamestringHeadportrait,intGradestringMajorintUserClass,stringAddress) {Name =name; Headportrait=headportrait; Grade=grade; Major=Major; Class=UserClass; Address=address; }    }
View Code

Backstage HomeController a method inside: (using JSON to pass data)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Security;usingTest.models;usingSystem.Text.RegularExpressions;usingSystem.IO;usingSystem.Web.Script.Serialization;namespacetest{ Public classHomecontroller:controller {//entity:entity Framework Data context Access InstanceWebappcontext entity =NewWebappcontext (); /// <summary>        ///getting user information asynchronously/// </summary>        /// <returns></returns>[HttpPost] PublicActionResult Ajaxgetuserinfos () {varUserinfos =NewList<userinfo>(); varUsers = entity. Users.where (m = m.address.length >0). (M =m.address); foreach(varUserinchusers) {Userinfos.add (NewUserInfo (user. Name, user. headportrait, user. Grade, user. Major, user. Class, user.            Address)); }            returnJson (NewJavaScriptSerializer ()).        Serialize (Userinfos)); }    }}
View Code

Foreground a JS method: (using AJAX to load user data asynchronously to the foreground)

//using AJAX to get datafunctionAjaxgetdata () {$.ajax ({URL:'/home/ajaxgetuserinfos ', type:' Post ', Async:true, Success:function(data) {varUserdatas = eval (' (' + data + ') '); varn =userdatas.length; vari = 0;  for(i = 0; i < n; i++)            {                //Adduserinfo: Your first function to add user information to the page, which can be rewritten according to your needsAdduserinfo (Userdatas[i]. Name, Userdatas[i]. Headportrait, Userdatas[i]. Grade, Userdatas[i]. Major, Userdatas[i]. Class, Userdatas[i].            Address); }}, Error:function() {alert ("Data load failed, please refresh page retry!"); }    });}
View Code

This example is the foreground request data, in the background serialization, foreground deserialization, of course, the foreground can also send data, in front of the foreground will be passed the data serialized into JSON data, in the background to deserialize.

This knowledge sharing is here, please look forward to the next share. ^_^

< my blog homepage;:http://www.cnblogs.com/forcheng/

<wing Studio homepage;:http://www.wingstudio.org/

ASP. NET MVC Web site development Summary (vi)--Brief talk on the serialization and deserialization of 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.