使用 VB.NET 解析简单的 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20388270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:01:22  来源:igfitidea点击:

Parse Simple JSON with VB.NET

jsonvb.netparsingdatacontractserializercisco

提问by Rookie

I have a simple JSON string that I am trying to parse with Visual Studio Express 2010 using native .net 4.0 instead of NewtonSoft. The json data that I am trying to parse looks like the following.

我有一个简单的 JSON 字符串,我正在尝试使用本机 .net 4.0 而不是 NewtonSoft 用 Visual Studio Express 2010 解析它。我试图解析的 json 数据如下所示。

"{"token_type":"Bearer",""expires_in":3599,"access_token":"VxwK6YWYj6paqyMK2D2r4uDl34qg"}"

I can get the following code to run without error but when I try to dump the contents of the object I don't have any in the list. Here is the class I created.

我可以让以下代码正常运行,但是当我尝试转储对象的内容时,列表中没有任何内容。这是我创建的类。

Public Class AuthToken

  Public Property token_type As String
      Get
          Return m_token_type
      End Get
      Set(ByVal value As String)
          m_token_type = value
      End Set
  End Property
  Private m_token_type As String
  Public Property expires_in() As Integer
      Get
          Return m_expires_in
      End Get
      Set(ByVal value As Integer)
          m_expires_in = value
      End Set
  End Property
Private m_expires_in As String
Public Property access_token As String
      Get
          Return m_access_token
      End Get
      Set(ByVal value As String)
          m_access_token = value
      End Set
 End Property
 Private m_access_token As String
End Class

My feeling is that my problem is in my class but I am not sure. So after looking for hours on this site and others Trouble parsing Json into .net ObjectI have put together the following code to parse the information and dump it to a RichTextBox just to see what it is.

我的感觉是我的问题出在我的班级上,但我不确定。因此,在此站点和其他站点上查找了几个小时后,将Json 解析为 .net 对象时遇到了麻烦,我将以下代码放在一起以解析信息并将其转储到 RichTextBox 中,以查看它是什么。

Dim sr As New StreamReader(req.GetResponse.GetResponseStream)
Dim authtoken As New List(Of AuthToken)()
Dim ms As New MemoryStream(System.Text.Encoding.Unicode.GetBytes(sr.ReadToEnd))
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(authtoken.GetType)
authtoken = DirectCast(serializer.ReadObject(ms), List(Of AuthToken))
ms.Close()
ms.Dispose()
sr.Close()
sr.Dispose()
For Each token In authtoken
        rtbResponse.AppendText("Token: " & token.access_token & " Expires in: " & token.expires_in)
Next

So is my class created incorrectly? Is the data from the memorystream just not getting into the authtoken object because the class doesn't match the contents of the json data as it is deserialized?

那么我的班级创建不正确吗?内存流中的数据是否只是没有进入 authtoken 对象,因为该类在反序列化时与 json 数据的内容不匹配?

If I am using the "DataContractSerializer" do I need to have data contract "stuff" in my class?

如果我正在使用“DataContractSerializer”,我的课程中是否需要有数据合同“东西”?

Any help is GREATLY appreciated.

任何帮助是极大的赞赏。

采纳答案by rinukkusu

The data is not getting into the authtoken variable because you don't have a list of auth tokens there - only one object. So if you get those JSON objects one at the time get rid of your lists and do it like that:

数据没有进入 authtoken 变量,因为那里没有 auth 令牌列表 - 只有一个对象。因此,如果您当时获得了那些 JSON 对象,请删除您的列表并这样做:

Dim sr As New StreamReader(req.GetResponse.GetResponseStream)
Dim authtoken As AuthToken
Dim ms As New MemoryStream(System.Text.Encoding.Unicode.GetBytes(sr.ReadToEnd))
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(AuthToken))

authtoken = DirectCast(serializer.ReadObject(ms), AuthToken)

ms.Close()
ms.Dispose()
sr.Close()
sr.Dispose()

rtbResponse.AppendText("Token: " & authtoken.access_token & " Expires in: " & authtoken.expires_in)