复制代码 代码如下:
1.DataContractSerializer
2.JavascriptSerializer
复制代码 代码如下:
<DataMember()> _
Public Class Model
<DataMember()> Public Property PropertyA As String
<DataMember()> Public Property PropertyB As String
<IgnoreDataMember()> Public Property PropertyC As String
End Class
复制代码 代码如下:
$.ajax({
type: "POST",
url: "Default.aspx/GetJSON",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg);
}
});
复制代码 代码如下:
<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]
其中Model类定义如下:
[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
复制代码 代码如下:
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
新闻热点
疑难解答
图片精选