反序列化JSON后的NullPointer

时间:2018-12-30 01:00:05

标签: json vb.net json.net

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

运行时,我在Person类中得到了一个N​​ullPointer,因此我怀疑反序列化无法正常工作。

首先使用我的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

1 个答案:

答案 0 :(得分:1)

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

产生问题的是Person类构造函数和Transform方法的组合:当反序列化器创建一个新类时,它将使用空值初始化。因此,Transform方法在尝试操作空字符串时将引发NullReferenceException

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

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