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

 

你可能感兴趣的:(VB.NET使用JSON.NET集合序列及反序列实例)