引用imports Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq
1.1、解析JSON字串:
dim empData as string="
{“message”:null,“data”:{“paginationData”:[{“name”:“管理员”,“user_id”:16394,“groupsname”:""},{“name”:“测试”,“user_id”:16395,“groupsname”:“部门A”},{“name”:“隔壁老王”,“user_id”:16396,“groupsname”:“部门A”},{“name”:“小明”,“user_id”:16397,“groupsname”:“部门B”}],“result”:1}
"
Dim p As JObject = CType(JsonConvert.DeserializeObject(empData), JObject)
1.2、读取值:
dim msg as string=p(“message”).ToString()
1.3、职员列表解析
Public Structure Employee
Dim name As String
Dim user_id As Integer
Dim groupsname As String
End Structure
Dim emp As List(Of Employee)
emp = JsonConvert.DeserializeObject(Of List(Of Employee))(p(“data”)(“paginationData”).ToString)
1.4、读取职员信息
dim empCount as integer=emp.count '全部职员数量
msgbox emp(2).name & emp(2).user_id & emp(2).groupsname '结果为“隔壁老王16396部门A”
1.5、要想实现自动将JSON的职员数组解析到Employee列表里,必须将Employee结构内的各变量定义的和JSON字串里一致。
比如,在JSON字串中是name、user_id、groupsname,那么在结构里也必须这样定义。