asp.net JSON Serialization Technology Analysis _ practical skills

Source: Internet
Author: User
Tags class definition serialization
Starting at ASP.net 3.5,. NET platform supports two kinds of serialization methods:
Copy Code code as follows:

1.DataContractSerializer
2.JavascriptSerializer

The former is mainly serialized according to the Data Contract (DataContract), through a data contract defined as follows:
Code
Copy Code code as follows:

<datamember () > _
Public Class Model
<datamember () > Public Property Propertya as String
<datamember () > Public Property Propertyb as String
<ignoredatamember () > Public Property PROPERTYC as String
End Class

This will determine whether the property is serialized according to <DataMember> and <IgnoreDataMember>.
The disadvantage of data contract serialization is that it is necessary to determine whether a property is serialized at the beginning of a data class definition, and that the data model used everywhere may result in the need to be all specified as DataMember. A property that has a default value (such as nothing or null) is also serialized as an unwanted attribute; second, serialization cannot use property aliases, the names defined in the business may be too long, and if a property alias cannot be specified, the bandwidth is wasted, resulting in slower communication between the server side and the client.
Pagemethod is the best use scenario for using the JSON serialization technique to implement asynchronous calls (Ajax calls) on a page without having to write all the logic in a service, and the business logic of the UI can be written in Pagemethod. The Pagemethod can be opened by inserting the following ScriptManager declaration and specifying Enablepagemethods as true.
<asp:scriptmanager id= "ScriptManager1" runat= "Server" enablepagemethods= "true" >
</asp:ScriptManager>
Many people see instances of Pagemethod calls through Ms Ajax, but because Microsoft has been doing asp.net and jquery collaboration experiments in the last two years, we can actually invoke Pagemethod through jquery. However, the call method of jquery cannot use get, as long as the post null JSON object implements the class get call. The script is as follows:
Copy Code code as follows:

$.ajax ({
Type: "POST",
URL: "Default.aspx/getjson",
Data: "{}",
ContentType: "Application/json; Charset=utf-8 ",
DataType: "JSON",
Success:function (msg) {
Alert (msg);
}
});

The Pagemethod life method is as follows:
Copy Code code as follows:

<webmethod () > _
Public Shared Function Getjson () as String
Dim list as New list (of Model)
For index as Integer = 0 to 1000
Dim M_json as New Model with {. Propertya = "Hello",. Propertyb = "World"}
List. ADD (M_json)
Next
Dim str as String = jsonhelper.serialize (list)
Return str
End Function
[Code]
The model class is defined as follows:
[Code]
<datacontract () > _
Public Class Model
<datamember (Emitdefaultvalue:=false, Isrequired:=false, name:= "A") > Public Property Propertya as String
<datamember (Emitdefaultvalue:=false, Isrequired:=false, name:= "B") > Public Property Propertyb as String
<datamember (Emitdefaultvalue:=false, Isrequired:=false, name:= "C") > Public Property PROPERTYC as String
End Class

The serialized model instance results are:
{"A": "Hello", "B": "World"}
Here to provide you with a more popular online JSON conversion class, using the JavaScriptSerializer, the code is as follows:
Copy Code code as follows:

public class Jsonhelper
public Shared Function Serialize (of T) (ByVal obj As T) as String
Dim serializer as New System.Runtime.Serialization.Json.DataContractJsonSerializer (obj. GetType ())
Dim MS as New MemoryStream ()
Serializer. WriteObject (MS, obj)
Dim retVal as String = Encoding.Default.GetString (ms. ToArray ())
return retVal
End Function
Public Shared Function deserialize (of T) (ByVal JSON as String) as T
Dim obj As T = Activator.CreateInstance (Of T) ()
Dim ms as New MemoryStream (Encoding.Unicode.GetBytes (JSON))
Dim Serializer as New System.Runtime.Serialization.Json.DataContractJsonSerializer (obj. GetType ())
obj = CType (serializer. ReadObject (MS), T)
Ms. Close ()
return obj
End Function
End Class
Related Article

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.