ASP.NET JSON 基本 Serialization & Deserialization

 
記得係 project 上增加 Reference -> System.ServiceModel.Web
 

   default.aspx
 
   <form id="form1" runat="server">
   <div>
      <asp:Label ID="Label1" runat="server" Text=""></asp:Label><BR>
      <asp:Label ID="Label2" runat="server" Text=""></asp:Label><BR>
      <asp:Label ID="Label3" runat="server" Text=""></asp:Label><BR>
   </div>
   </form>
 

 

   default.aspx
 
   Imports System
   Imports System.Runtime.Serialization.Json
   Imports System.IO
 
   Partial Public Class _Default
      Inherits System.Web.UI.Page
 
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
         Dim p1 As New Staff()
         p1.Name = "Ma"
         p1.Age = 29
         p1.Position = "Programmer"
 
         Label1.Text = "Original : " & p1.Name & " " & p1.Age & " " & p1.Position
 
         Dim Stream1 As New MemoryStream()
         Dim ss1 As New DataContractJsonSerializer(GetType(Staff))
 
         ss1.WriteObject(Stream1, p1)
 
         Stream1.Position = 0
 
         Dim sr As New StreamReader(Stream1)
         Label2.Text = "Serization : " & sr.ReadToEnd()
 
         Stream1.Position = 0
 
         Dim p2 As New Staff
         p2 = TryCast(ss1.ReadObject(Stream1), Staff)
 
         Label3.Text = "Deserization : " & p2.Name & " " & p2.Age & " " & p2.Position
 
      End Sub
 
      <SerializableAttribute()> Private Class Staff
 
         Public Name As String
         Public Position As String
         Public Age As String
 
      End Class
 
   End Class