反序列化JSON继续获得null referance异常

时间:2015-06-13 20:07:37

标签: json vb.net-2010

好的,所以我想从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

谢谢。

2 个答案:

答案 0 :(得分:0)

我不是.Net开发人员,但我可以建议你应该检查身份验证详细信息,同时将此链接称为https,这意味着它的安全行,因此可能拒绝您的连接。 Link

答案 1 :(得分:0)

用于反序列化的对象类是错误的,您可以使用jsonutils.com从json字符串或URL生成类。

以下是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)