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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB.NET使用 Newtonsoft.Json 序列化结构体的过程与序列化类的过程类似。你可以使用 `JsonConvert.SerializeObject` 方法将结构体对象转换为 JSON 字符串,以及使用 `JsonConvert.DeserializeObject` 方法将 JSON 字符串转换回结构体对象。 以下是一个示例代码,演示如何使用 Newtonsoft.Json 序列化结构体: ```vb Imports Newtonsoft.Json ' 定义一个结构体 Public Structure Person Public Name As String Public Age As Integer End Structure Sub Main() ' 创建一个结构体对象 Dim person As New Person With { .Name = "John", .Age = 25 } ' 序列化结构体为 JSON 字符串 Dim json As String = JsonConvert.SerializeObject(person) ' 输出序列化后的 JSON 字符串 Console.WriteLine(json) ' 序列JSON 字符串为结构体对象 Dim deserializedPerson As Person = JsonConvert.DeserializeObject(Of Person)(json) ' 输出序列化后的结构体对象的属性值 Console.WriteLine(deserializedPerson.Name) Console.WriteLine(deserializedPerson.Age) End Sub ``` 在这个示例中,我们首先定义了一个名为 `Person` 的结构体,其中包含了 `Name` 和 `Age` 两个属性。 然后,我们创建了一个 `Person` 结构体对象,并设置了其属性值。 接下来,使用 `JsonConvert.SerializeObject` 方法将结构体对象序列化为 JSON 字符串,并将结果存储在 `json` 变量中。 然后,我们输出序列化后的 JSON 字符串。 最后,使用 `JsonConvert.DeserializeObject` 方法将 JSON 字符串序列化为 `Person` 结构体对象,并将结果存储在 `deserializedPerson` 变量中。 最后,我们输出序列化后的结构体对象的属性值。 确保在项目中引用了 Newtonsoft.Json 库,并在代码文件中导入了命名空间 `Newtonsoft.Json`。 希望这个示例能够帮助你在 VB.NET使用 Newtonsoft.Json 序列化结构体。如果你还有其他问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值