VB.NET JSON的使用

首先要下载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))
最后就可以获取自己想要的值了

你可能感兴趣的:(VB.NET JSON的使用)