i have a nested json file that i want to deserialize, i keep getting an error " Object reference not set to an instance of an object."
here is my json file
[
{
"Name": "Junius",
"LastName": "Lekgwara",
"CellNo": [
{
"CellC": "072685345",
"Voda": "0728589303"
}
]
},
{
"Name": "Phuti",
"LastName": "Gravel",
"CellNo": [
{
"CellC": "08377777777",
"Voda": "089888888888"
}
]
},
{
"Name": "Donald",
"LastName": "Gravel",
"CellNo": [
{
"CellC": "0791408989",
"Voda": "0117689009"
}
]
}
]
here is my Person class
Public Class Person
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _LastName As String
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Public Property cellno As CellNo
End Class
Public Class CellNo
Public Property CellC As String
Public Property Voda As String
End Class
PersonList class
Public Class PersonList
Private _ListPerson As List(Of Person)
Public Property ListPerson() As List(Of Person)
Get
Return _ListPerson
End Get
Set(ByVal value As List(Of Person))
_ListPerson = value
End Set
End Property
Sub New()
_ListPerson = New List(Of Person)
End Sub
End Class
reading json file, when this sub is called i get an error
Public Async Sub LoadFromFile()
Dim reader As String = Await GetFile()
Dim serializer As DataContractJsonSerializer = New DataContractJsonSerializer(GetType(ObservableCollection(Of Person)))
Dim ms As MemoryStream = New MemoryStream(Encoding.UTF8.GetBytes(reader))
Dim listOf As ObservableCollection(Of Person) = New ObservableCollection(Of Person)
listOf = serializer.ReadObject(ms)
For Each item In listOf
list.ListPerson.Add(item)
Debug.WriteLine(item.Name + " " + item.LastName + " " + item.cellno.CellC + " " + item.cellno.Voda) 'Object reference not set to an instance of an object, on item.cellno.CellC and item.cellno.Voda
Next
End Sub
i want to be able to reference it like
Dim person As Person = New Person()
lblName.Text = person.Name
lblLastName.Text = person.LastName
lblCellCNo.Text= person.cellno.CellC
lblCellVodaNo.Text= person.cellno.Voda
how can i fix this?
Solved the problem by using CellNo as an array
`"CellNo":[
"072685345",
"0728589303"
]
For Each item In listOf
list.ListPerson.Add(item)
Debug.WriteLine(item.Name + " " + item.LastName)
For Each i In item.CellNo
Debug.WriteLine(i)
Next
Next
display
Junius Lekgwara
Cell No: 072685345
Cell No: 0728589303
Phuti Gravel
Cell No: 08377777777
Cell No: 089888888888
Donald Gravel
Cell No: 0791408989
Cell No: 0117689009