コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
"{
""Id"":""1"",
""Name"":""山田太郎"",
""Age"":""20"",
""Gender"":""1"",
""
}"
Imports System.Runtime.Serialization
<DataContract()>
Public Class Customer
<DataMember()>
Public Property Id As Integer = 0
<DataMember()>
Public Property Name As String = String.Empty
<DataMember()>
Public Property Age As Integer = 0
<DataMember()>
Public Property Gender As Integer = 0
End Class
Imports System.Runtime.Serialization.Json
Public Class JsonUtility
''' <summary>
''' json を読み取り、クラスに変換して返す
''' </summary>
''' <typeparam name="T">変換後の型</typeparam>
''' <param name="json">json文字列</param>
''' <returns></returns>
Public Shared Function GetObject(Of T)(ByVal json As String) As T
Dim result As T
Dim tp As Type = GetType(T)
Dim serializer As New DataContractJsonSerializer(tp)
Using stream As New IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json))
result = DirectCast(serializer.ReadObject(stream), T)
End Using
Return result
End Function
End Class
Private Sub btnReadJson_Click(sender As Object, e As EventArgs) Handles btnReadXml.Click
Dim json As New Text.StringBuilder()
Dim entity As Customer
json.AppendLine("{")
json.AppendLine("""Id"": ""1"",")
json.AppendLine("""Name"":""山田太郎"",")
json.AppendLine("""Age"":""20"",")
json.AppendLine("""Gender"":""1"",")
json.AppendLine("}")
entity = JsonUtility.GetObject(Of Customer)(json.ToString())
Console.WriteLine(entity.ToString())
End Sub
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。