反序列化 JSON 后的空指针



我试图填充一个List(of Person)Person是我创建的类,使用文件中的JSON数据。
我设法从文件中提取数据并将其保存为字符串。
从这个字符串中,我想反序列化以获取我的List(of Person).

运行时,我在Person类中得到一个 NullPointer ,所以我怀疑反序列化无法正常工作。

首先关闭我的 JSON:

[
  {
    "fname": "Lucas",
    "lname": "Films",
    "birthday": "1993-03-04T00:00:00",
  },
  {
    "fname": "John",
    "lname": "Malkovic",
    "birthday": "1993-04-01T00:00:00",
  }
]

我的Person课:

Public Class Person
Private _fname, _lname As String
Private _birthday As Date
Public Sub New(_fname As String, _lname As String, _birthday As Date)
    fname = _fname
    lname = _lname
    birthday = _birthday
End Sub
Property fname As String
    Get
        Return _fname
    End Get
    Set(value As String)
        _fname = Transform(value)
    End Set
End Property
Property lname As String
    Get
        Return _lname
    End Get
    Set(value As String)
        _lname = Transform(value)
    End Set
End Property
Property birthday As Date
    Get
        Return _birthday
    End Get
    Set(value As Date)
        Try
            _birthday = Transform(value)
        Catch
            Throw New Exception("DD.MM.YYYY")
        End Try
    End Set
End Property
Private Function Transform(inp As String) As String
    inp = inp(0).ToString().ToUpper + inp.Substring(1).ToLower
    Return inp
End Function
End Class

读取 JSON 文件的子文件:

Public records As New List(Of Person)
Public Sub read()
    Dim openfiledialog1 As New OpenFileDialog
    openfiledialog1.Filter = "json dateien | *.json"
    If openfiledialog1.ShowDialog = DialogResult.OK Then
        Dim jtext As String = IO.File.ReadAllText(openfiledialog1.FileName)
        records = JsonConvert.DeserializeObject(Of List(Of Person))(jtext)
    End If
End Sub

我不知道是否需要它,但这也是在Button.Click之后运行的潜艇,并让一切运转起来:

Private Sub btjson_Click(sender As Object, e As EventArgs) Handles btjson.Click
    inst = New jsoncrud
    inst.read()
    Dim value As List(Of Person) = inst.records
    Dim frm = New showdata(value)
    Me.Visible = False
    frm.showdialog()
    Me.Close()
End Sub

JSON 有效,反序列化工作正常。

产生问题的是Person类构造函数与 Transform 方法的组合:当反序列化程序创建新类时,将使用 null 值对其进行初始化。因此,Transform 方法在尝试操作 null 字符串时将引发NullReferenceException

如果出于其他原因需要该构造函数(JSON 反序列化程序不需要它),请在 Transform 方法中添加 null 检查:

Private Function Transform(inp As String) As String
    If inp Is Nothing Then Return String.Empty
    Return inp(0).ToString().ToUpper & inp.Substring(1).ToLower
End Function

我还建议添加一些JsonProperty属性,以便您可以为Person类属性分配更有意义的名称:

Public Class Person2
    Private _fname As String = String.Empty
    Private _lname As String = String.Empty
    Private _birthday As Date = nothing
    Public Sub New(_firstname As String, _lastname As String, _brthday As Date)
        FirstName = _firstname
        LastName = _lastname
        BirthDay = _brthday
    End Sub
    <JsonProperty("fname")>
    Property FirstName As String
        Get
            Return _fname
        End Get
        Set(value As String)
            _fname = Transform(value)
        End Set
    End Property
    <JsonProperty("lname")>
    Property LastName As String
        Get
            Return _lname
        End Get
        Set(value As String)
            _lname = Transform(value)
        End Set
    End Property
    <JsonProperty("birthday")>
    Property BirthDay As Date
        Get
            Return _birthday
        End Get
        Set(value As Date)
            Try
                _birthday = value
            Catch
                Throw New Exception("DD.MM.YYYY")
            End Try
        End Set
    End Property
    Private Function Transform(inp As String) As String
        If inp Is Nothing Then Return String.Empty
        Return inp(0).ToString().ToUpper & inp.Substring(1).ToLower
    End Function
End Class

或者,您可以使用这个简化的类,因为名称似乎已经格式化为正确的大小写

Public Class Person
    <JsonProperty("fname")>
    Public Property FirstName As String
    <JsonProperty("lname")>
    Public Property LastName As String
    <JsonProperty("birthday")>
    Public Property Birthday As Date
End Class

相关内容

  • 没有找到相关文章

最新更新