VB.NET JSON的使用

本文介绍如何使用Newtonsoft.Json库在VB.NET中进行JSON数据的序列化与反序列化操作,包括基本的JSON字符串处理及复杂的数据结构转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先要下载Newtonsoft.Json,如果你用的是VisualStudio 2017,可以右击【解决方法】选择点击【管理解决方案的NuGet程序包】,在浏览的搜索框内输入“Newtonsoft.Json”,根据你需要的版本下载程序包。
简单应用
1 定义JSON字符串
Dim empData As String = “{‘name’:‘Tom’,‘user_id’:16394,‘groupsname’:‘HardWorkTeam’}”
2 JSON字符串转化成JSON对象
Dim jsonResult As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(empData)
3 根据key值获得value值
Dim objectStr As String = jsonResult.Item(“name”).ToString
JSON对象数组的应用
1 定义学生结构
Public Structure Student
Dim stuName As String
Dim stuAge As Integer
Dim stuSex As String
End Structure
2 赋值
Dim stu1 As Student
Dim stu2 As Student
Dim stu3 As Student
stu1.stuName =“lucy”
stu1.stuAge =10
stu1.stuSex =“female”
stu2.stuName =“lily”
stu2.stuAge =12
stu2.stuSex =“female”
stu3.stuName =“Tom”
stu3.stuAge =14
stu3.stuSex =“male”
3 定义List并填值
Dim stuList As List (of Student) = New List(of Student)
stuList.Add(stu1)
stuList.Add(stu2)
stuList.Add(stu3)
4 序列化JSON对象数组为JSON字符串
JsonConvert.SerializeObject(stuList)
5 反序列化获得JSON对象数组
JsonConvert.DeserializeObject(JsonConvert.SerializeObject(emp))
最后就可以获取自己想要的值了
--------------------- 
作者:远志烛 
来源:CSDN 
原文:https://blog.csdn.net/weixin_43302289/article/details/84340725 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值