見出し画像

VB.NetでJSONを扱う方法

VB.NetでJSONを扱う方法

JSONは、JavaScript Object Notationの略で、Webアプリケーションでデータを交換するために広く使用されています。VB.Netでは、System.Web.Script.Serialization名前空間に含まれるJavaScriptSerializerクラスを使用してJSONデータをシリアル化および逆シリアル化することができます。この記事では、VB.NetでJSONデータを扱う方法について説明します。

シリアル化

JavaScriptSerializerクラスのSerialize()メソッドを使用して、VB.NetオブジェクトをJSON形式に変換できます。以下は、JsonObjectクラスを使用してJSONオブジェクトを作成する例です。

Imports System.Web.Script.Serialization

' JSONオブジェクトを表すクラス
Public Class JsonObject
    Public Property Id As Integer
    Public Property Name As String
    Public Property Price As Decimal
    Public Property Lots As Integer
    Public Property UpdDate As Date
End Class

' JSONをシリアル化する
Dim json As String = New JavaScriptSerializer().Serialize(New JsonObject With {
    .Id = 1,
    .Name = "Product 1",
    .Price = 9.99,
    .Lots = 100,
    .UpdDate = DateTime.Now
})

上記のコードでは、提供された項目を表すJsonObjectクラスを使用してJSONオブジェクトを作成し、JavaScriptSerializer.Serialize()メソッドを使用してJSONにシリアル化しています。

逆シリアル化

JSONをVB.Netオブジェクトに逆シリアル化するには、JavaScriptSerializerクラスのDeserialize()メソッドを使用します。以下は、JSONファイルをオブジェクトに逆シリアル化する例です。

Imports System.IO
Imports System.Web.Script.Serialization

' JSONオブジェクトを表すクラス
Public Class JsonObject
    Public Property Id As Integer
    Public Property Name As String
    Public Property Price As Decimal
    Public Property Lots As Integer
    Public Property UpdDate As Date
End Class

' JSONファイルを読み込み、オブジェクトに逆シリアル化する
Dim jsonText As String = File.ReadAllText("path/to/json/file.json")
Dim jsonObject As JsonObject = New JavaScriptSerializer().Deserialize(Of JsonObject)(jsonText)

上記のコードでは、File.ReadAllText()メソッドを使用してJSONファイルを読み込み、JavaScriptSerializer.Deserialize()メソッドを使用してJSONを逆シリアル化しています。jsonObjectには、JSONファイルに含まれるデータが関連付けられます。

入れ子構造

JSONオブジェクトが入れ子になっている場合は、最初のオブジェクトから順にアクセスしていきます。以下は、入れ子になったJSONオブジェクトを扱う例です。

Dim json As String = "{""name"":""John Smith"",""age"":30,""address"":{""street"":""123 Main St."",""city"":""Anytown"",""state"":""CA"",""zip"":""12345""}}"
Dim jss As New JavaScriptSerializer()
Dim data As Object = jss.DeserializeObject(json)
Dim name As String = data("name").ToString()
Dim age As Integer = data("age")
Dim address As Object = data("address")
Dim street As String = address("street").ToString()
Dim city As String = address("city").ToString()
Dim state As String = address("state").ToString()
Dim zip As String = address("zip").ToString()

上記の例では、逆シリアル化したJSONオブジェクトの各プロパティにアクセスしています。

ファイル出力

VB.NetでJSONファイルを作成するには、System.IO名前空間のStreamWriterを使用します。以下は、JSONファイルを作成する例です。

Imports System.IO
Imports System.Web.Script.Serialization

' JSONオブジェクトを表すクラス
Public Class JsonObject
    Public Property Id As Integer
    Public Property Name As String
    Public Property Price As Decimal
    Public Property Lots As Integer
    Public Property UpdDate As Date
End Class

' JSONファイルを作成する
Dim json As String = New JavaScriptSerializer().Serialize(New JsonObject With {
    .Id = 1,
    .Name = "Product 1",
    .Price = 9.99,
    .Lots = 100,
    .UpdDate = DateTime.Now
})

Dim filePath As String = "path/to/json/file.json"
Using writer As StreamWriter = File.CreateText(filePath)
    writer.Write(json)
End Using

上記の例では、StreamWriterを使用してJSONファイルを作成しています。

ファイル読み取り

JSONファイルをVB.Netで読み込むには、System.IO名前空間のStreamReaderを使用します。以下は、JSONファイルを読み込む例です。

Imports System.IO
Imports System.Web.Script.Serialization

' JSONオブジェクトを表すクラス
Public Class JsonObject
    Public Property Id As Integer
    Public Property Name As String
    Public Property Price As Decimal
    Public Property Lots As Integer
    Public Property UpdDate As Date
End Class

' JSONファイルを読み込んでオブジェクトに逆シリアル化する
Dim filePath As String = "path/to/json/file.json"
Using reader As StreamReader = New StreamReader(filePath)
    Dim jsonText As String = reader.ReadToEnd()
    Dim jsonObject As JsonObject = New JavaScriptSerializer().Deserialize(Of JsonObject)(jsonText)
End Using

上記の例では、StreamReaderを使用してJSONファイルを読み込み、JavaScriptSerializer.Deserialize()メソッドを使用してJSONを逆シリアル化しています。

この記事が気に入ったらサポートをしてみませんか?