Starting from ASP. NET 3.5, the. NET platform supports two serialization methods:
CopyCode The Code is as follows: 1. datacontractserializer
2. javascriptserializer
The former is mainly serialized Based on the Data contract (datacontract), through the following definition of the Data contract:
CodeCopy codeThe Code is 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
The attributes are serialized Based on <datamember> and <ignoredatamember>.
Serialization Based on Data contracts has disadvantages. Its disadvantage lies in the need to determine whether to serialize attributes at the beginning of data class definition, and the data model used everywhere) this may cause all attributes to be specified as datamember, and useless attributes with default values (such as nothing or null) are also serialized. Second, attribute aliases cannot be used for serialization, the name defined in the service may be too long. If the attribute alias cannot be specified, the bandwidth will be wasted, resulting in slow communication between the server and the client.
Pagemethod is the best use case of JSON serialization technology. It implements asynchronous calls (Ajax calls) on pages without writing all the logic into the service, business logic judgment and processing on the UI can be written in pagemethod. Insert the following scriptmanager Statement on the screen and specify enablepagemethods as true to enable pagemethod.
<Asp: scriptmanager id = "scriptmanager1" runat = "server" enablepagemethods = "true">
</ASP: scriptmanager>
Many people see examples of pagemethod calling through Ms Ajax, but Microsoft has been working on ASP in the past two years. net and jquery, we can also call pagemethod through jquery. However, get cannot be used for jquery call methods. As long as the post object is empty, the class get call can be implemented. The script is as follows: Copy code The Code is as follows: $. Ajax ({
Type: "Post ",
URL: "default. aspx/getjson ",
Data :"{}",
Contenttype: "application/JSON; charset = UTF-8 ",
Datatype: "JSON ",
Success: function (MSG ){
Alert (MSG );
}
});
pagemethod: copy Code the code is as follows: _< br> 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 {. 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]
_< br> public class model
Public Property propertya as string
public property propertyb as string
public property propertyc as string
end class
serialized model instance result:
{"A": "hello", "B ": "world"}
here is a popular JSON conversion class on the Internet, which uses javascriptserializer. The Code is as follows: copy Code the code is 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