VB.NET使用JSON.NET集合序列及反序列實例

VB.NET使用JSON.NET集合序列及反序列實例

Imports Newtonsoft.Json
Public Class Form1
    Public Sub New()
        ' 此調用是設計器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 調用之後添加任何初始化。

        Dim contentData As ContentData = New ContentData() With {
        .Content = "測試",
        .CLocationX = 10.2,
        .CLocationY = 20.2,
        .CFontName = "宋體",
        .CFontBold = "粗體",
        .CFontSize = 10.5
    }
        Dim contentData2 As ContentData = New ContentData() With {
        .Content = "22測試",
        .CLocationX = 10.2,
        .CLocationY = 40.2,
        .CFontName = "宋體",
        .CFontBold = "粗體",
        .CFontSize = 10.5
    }
        Dim contentList As List(Of ContentData) = New List(Of ContentData)()
        contentList.Add(contentData)
        contentList.Add(contentData2)
        Dim json As String = JsonConvert.SerializeObject(contentList, Formatting.Indented)
        Console.WriteLine(json)
'調試輸出結果:
'[
'  {
'    "Content": "測試",
'    "CLocationX": 10.2,
'    "CLocationY": 20.2,
'    "CFontName": "宋體",
'    "CFontBold": "粗體",
'    "CFontSize": 10.5
'  },
'  {
'    "Content": "22測試",
'    "CLocationX": 10.2,
'    "CLocationY": 40.2,
'    "CFontName": "宋體",
'    "CFontBold": "粗體",
'    "CFontSize": 10.5
'  }
']
        Dim imageData = New ImageData() With {
        .PWidth = 200,
        .PHeight = 300,
        .BImagePath = "",
        .SavePath = " D:\傳輸文件",
        .Data = json
    }
        Dim jsonString = JsonConvert.SerializeObject(imageData, Formatting.Indented)
        Console.WriteLine(jsonString)
'調試輸出結果:
'{
'  "PWidth": 200,
'  "PHeight": 300,
'  "BImagePath": "",
'  "SavePath": " D:\\傳輸文件",
'  "Data": "[\r\n  {\r\n    \"Content\": \"測試\",\r\n    \"CLocationX\": 10.2,\r\n    \"CLocationY\": 20.2,\r\n    \"CFontName\": \"宋體\",\r\n    \"CFontBold\": \"粗體\",\r\n    \"CFontSize\": 10.5\r\n  },\r\n  {\r\n    \"Content\": \"22測試\",\r\n    \"CLocationX\": 10.2,\r\n    \"CLocationY\": 40.2,\r\n    \"CFontName\": \"宋體\",\r\n    \"CFontBold\": \"粗體\",\r\n    \"CFontSize\": 10.5\r\n  }\r\n]"
}
        Dim ss = New ImageData
        Dim sss = JsonConvert.DeserializeAnonymousType(jsonString, ss)
        Dim reContentList As List(Of ContentData) = JsonConvert.DeserializeObject(Of List(Of ContentData))(sss.Data)
        Console.WriteLine(reContentList.Count)
'調試輸出結果:2
        Console.WriteLine(reContentList(0).Content)
'調試輸出結果:測試
        Console.WriteLine(reContentList(1).Content)
'調試輸出結果:22測試
    End Sub
End Class
Public Class ImageData 'EmployeeBean
    Public Property PWidth As Integer
    Public Property PHeight As Integer
    Public Property BImagePath As String
    Public Property SavePath As String
    Public Property Data As String
End Class
Public Class ContentData
    Public Property Content As String
    Public Property CLocationX As Single
    Public Property CLocationY As Single
    Public Property CFontName As String
    Public Property CFontBold As String
    Public Property CFontSize As Single
End Class

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章