public class myName
{
public string Name { get; set; }
public string Surname { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
string strJSON = null;
// JSON = [{"Name":"Weerachai","Surname":"Nukitram"},{"Name":"Adisorn","Surname":"Bunsong"}]
strJSON = " [{\"Name\":\"Weerachai\",\"Surname\":\"Nukitram\"}, {\"Name\":\"Adisorn\",\"Surname\":\"Bunsong\"}]";
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJSON));
ObservableCollection<myName> list = new ObservableCollection<myName>();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ObservableCollection<myName>));
list = (ObservableCollection<myName>)serializer.ReadObject(ms);
foreach (myName name in list)
{
Console.WriteLine("Name : " + name.Name.ToString());
Console.WriteLine("Surname : " + name.Surname.ToString());
Console.WriteLine("======================");
}
}
Result
Name : Weerachai
Surname : Nukitram
======================
Name : Adisorn
Surname : Bunsong
Code for VB.NET
การสร้าง Create JSON (Serialize)
1. การสร้าง JSON แบบข้อมูล Array ชุดเดียว
Public Class myName
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Surname() As String
Get
Return m_Surname
End Get
Set(value As String)
m_Surname = value
End Set
End Property
Private m_Surname As String
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim nameList As New List(Of myName)
Dim name As New myName()
name.Name = "Weerachai"
name.Surname = "Nukitram"
nameList.Add(name)
Dim str As New MemoryStream()
Dim ser As New DataContractJsonSerializer(nameList.GetType())
ser.WriteObject(str, nameList)
str.Position = 0
Dim sr As New StreamReader(str)
Me.lbDisplay.Text = sr.ReadToEnd()
End Sub
Result
[{"Name":"Weerachai","Surname":"Nukitram"}]
2. การสร้าง JSON แบบข้อมูล Array มากกว่า 1 ชุด
Public Class myName
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Surname() As String
Get
Return m_Surname
End Get
Set(value As String)
m_Surname = value
End Set
End Property
Private m_Surname As String
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim nameList As New List(Of myName)
Dim name As New myName()
name.Name = "Weerachai"
name.Surname = "Nukitram"
nameList.Add(name)
name = New myName()
name.Name = "Adisorn"
name.Surname = "Bunsong"
nameList.Add(name)
Dim str As New MemoryStream()
Dim ser As New DataContractJsonSerializer(nameList.GetType())
ser.WriteObject(str, nameList)
str.Position = 0
Dim sr As New StreamReader(str)
Me.lbDisplay.Text = sr.ReadToEnd()
End Sub
Public Class myName
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Surname() As String
Get
Return m_Surname
End Get
Set(value As String)
m_Surname = value
End Set
End Property
Private m_Surname As String
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strJSON As String = Nothing
' JSON = {"Name":"Weerachai","Surname":"Nukitram"}
strJSON = " {""Name"":""Weerachai"",""Surname"":""Nukitram""}"
Dim name As myName = ReadToObject(strJSON)
Console.WriteLine("Name : " & name.Name.ToString())
Console.WriteLine("Surname : " & name.Surname.ToString())
End Sub
Public Shared Function ReadToObject(json As String) As myName
Dim deserializedMember As New myName()
Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(json))
Dim ser As New DataContractJsonSerializer(deserializedMember.GetType())
deserializedMember = TryCast(ser.ReadObject(ms), myName)
ms.Close()
Return deserializedMember
End Function
Public Class myName
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Surname() As String
Get
Return m_Surname
End Get
Set(value As String)
m_Surname = value
End Set
End Property
Private m_Surname As String
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strJSON As String = Nothing
' JSON = [{"Name":"Weerachai","Surname":"Nukitram"},{"Name":"Adisorn","Surname":"Bunsong"}]
strJSON = " [{""Name"":""Weerachai"",""Surname"":""Nukitram""}, {""Name"":""Adisorn"",""Surname"":""Bunsong""}]"
Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(strJSON))
Dim list As New ObservableCollection(Of myName)
Dim serializer As New DataContractJsonSerializer(GetType(ObservableCollection(Of myName)))
list = DirectCast(serializer.ReadObject(ms), ObservableCollection(Of myName))
For Each name As myName In list
Console.WriteLine("Name : " & name.Name.ToString())
Console.WriteLine("Surname : " & name.Surname.ToString())
Console.WriteLine("======================")
Next
End Sub
Result
Name : Weerachai
Surname : Nukitram
======================
Name : Adisorn
Surname : Bunsong
ตัวอย่างบทความที่มีการสร้าง .NET กับ JSON Windows Phone and JSON