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

VB.NET使用JSON.NET集合序列及vb.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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值