如何读取json返回字符串?

时间:2019-08-07 14:00:54

标签: json vb.net json.net

难以读取json有用的返回方式

搜索“遍历”以查找如何创建类以及如何反序列化json返回

这是json返回:

[[{"metadata":{},"contentType":0,"contentId":0,"objectName":"Mi","objectId":"1","classId":"118"},
{"metadata":{},"contentType":0,"contentId":0,"objectName":"BA","objectId":"224445","classId":"103"},
{"metadata":{},"contentType":0,"contentId":0,"objectName":"1","objectId":"239011","classId":"104"},
{"metadata":{},"contentType":0,"contentId":0,"objectName":"1","objectId":"239309","classId":"105"}]]

尝试了Visual Studio(VB.net)选择性粘贴以创建json类,但是我似乎无法理解如何使用它。使用Newtonsoft.Json。

这些是类,如何将json反序列化并使其有用?

Public Class Rootobject
    Public Property Property1()() As Class1
End Class

Public Class Class1
    Public Property metadata As Metadata
    Public Property contentType As Integer
    Public Property contentId As Integer
    Public Property objectName As String
    Public Property objectId As String
    Public Property classId As String
End Class

Public Class Metadata
End Class

1 个答案:

答案 0 :(得分:0)

您的JSON是Class1对象数组的数组,因此您需要对此进行反序列化。

Dim data As Class1()() = JsonConvert.DeserializeObject(Of Class1()())(json)

您实际上不需要这里的Rootobject类。

正在运行的演示:https://dotnetfiddle.net/1JuaYk