1

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?

Junius L
  • 15,881
  • 6
  • 52
  • 96

3 Answers3

1

The JSON shown is a bit different than the class structs you want to use. Person is a List or Array already in the JSON. CellNo is also an array even though it appears in that sample to only have one class object per person.

So, you can skip your PersonList entirely:

Public Class CellNo
    Public Property CellC As String
    Public Property Voda As String
End Class

Public Class Person  
    Public Property Name As String
    Public Property LastName As String
    Public Property CellNo As List(Of CellNo)
End Class

Deserialize to a List(Of Person):

Dim jstr = ... (wherever it comes from)

Dim jp As List(Of Person)
jp = JsonConvert.DeserializeObject(Of List(Of Person))(jstr)

This is now quite usable as is without further manipulations:

For Each P As Person In jp
    Console.WriteLine("{0}  Cell Voda:{1}", P.Name, P.CellNo(0).Voda)
Next

Junius Cell Voda: 0728589303
Phuti Cell Voda: 089888888888
Donald Cell Voda: 0117689009

Optionally, you can deserialize to an array of Person, and define Person.CellNo as an array also.


i'm not using newtonsoft

You get the same results using NET:

Imports System.Web.Script.Serialization

Dim jss As New JavaScriptSerializer
Dim jp As List(Of Person)

jp = jss.Deserialize(Of List(Of Person))(jstr)
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
0

See.. your problem is that
the item List is not well prepared for what you want.

 item.cellno HAS CHILD !!! 

and

 item.cellno.CellC 
 item.cellno.Voda

must be iterated..

 For Each item In listOf
    list.ListPerson.Add(item)
    'Debug.WriteLine(item.Name + " " + item.LastName + " " + _ 

   'you know that you have just 2 items in : CellNo, and play with that.
   For i= 0 to 1
      Item.CellNo(i) ' do something with ..
   End For

Next

also read this : vb.net datatable Serialize to json

Community
  • 1
  • 1
CristiC777
  • 481
  • 11
  • 20
0

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

Junius L
  • 15,881
  • 6
  • 52
  • 96