反序列化JSON 不断获取空引用异常
Deserialize JSON keep getting null referance exception
好的,所以我想从 https://openrct.net/ajax/chat.php 反序列化 json。问题是,无论我尝试什么,当我尝试访问存储的数据时,我都会遇到 Null Reference Exception。我已经尝试了将近一个小时,在谷歌上搜索并尝试了不同的东西,但我完全不知道该怎么做。请帮帮我。
Imports Newtonsoft
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Net
Public Class Form1
Dim WS As New WebClient
'/ajax/chat.php
'And /ajax/chat.php?latest=55 to get all chat messages after that ID
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Class PostWrapper
Public posts() As Post
End Class
Public Class Post
Public Property a() As String
Public Property m() As String
Public Property t() As String
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim json As String = WS.DownloadString("https://openrct.net/ajax/chat.php")
MsgBox(json)
Dim postWrapper = JsonConvert.DeserializeObject(Of PostWrapper)(json) ' Deserialize array of Post objects
MsgBox(postWrapper.posts(0).m) 'Errors Here.
End Sub
End Class
谢谢。
我不是 .Net 开发人员,但我可以建议您在调用此 link 作为其 https
时检查身份验证详细信息,这意味着它的安全线路,因此它可能会拒绝你的连接。 Link
你用于反序列化的对象 类 是错误的,你可以使用 jsonutils.com 从 json 字符串或 URL.[=12= 生成 类 ]
这是您的 json、
的 类
Public Class M
Public Property id As String
Public Property a As String
Public Property m As String
Public Property t As String
End Class
Public Class Example
Public Property m As M()
Public Property l As String
End Class
Dim postWrapper = JsonConvert.DeserializeObject(Of Example)(json)
MsgBox(postWrapper.m)
好的,所以我想从 https://openrct.net/ajax/chat.php 反序列化 json。问题是,无论我尝试什么,当我尝试访问存储的数据时,我都会遇到 Null Reference Exception。我已经尝试了将近一个小时,在谷歌上搜索并尝试了不同的东西,但我完全不知道该怎么做。请帮帮我。
Imports Newtonsoft
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Net
Public Class Form1
Dim WS As New WebClient
'/ajax/chat.php
'And /ajax/chat.php?latest=55 to get all chat messages after that ID
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Class PostWrapper
Public posts() As Post
End Class
Public Class Post
Public Property a() As String
Public Property m() As String
Public Property t() As String
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim json As String = WS.DownloadString("https://openrct.net/ajax/chat.php")
MsgBox(json)
Dim postWrapper = JsonConvert.DeserializeObject(Of PostWrapper)(json) ' Deserialize array of Post objects
MsgBox(postWrapper.posts(0).m) 'Errors Here.
End Sub
End Class
谢谢。
我不是 .Net 开发人员,但我可以建议您在调用此 link 作为其 https
时检查身份验证详细信息,这意味着它的安全线路,因此它可能会拒绝你的连接。 Link
你用于反序列化的对象 类 是错误的,你可以使用 jsonutils.com 从 json 字符串或 URL.[=12= 生成 类 ]
这是您的 json、
的 类Public Class M
Public Property id As String
Public Property a As String
Public Property m As String
Public Property t As String
End Class
Public Class Example
Public Property m As M()
Public Property l As String
End Class
Dim postWrapper = JsonConvert.DeserializeObject(Of Example)(json)
MsgBox(postWrapper.m)