locked
Working with nested json file

    Question

  • Hi, 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

    how can i fix this?

    Monday, January 26, 2015 9:33 AM

Answers

  • I have solved it 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

    • Marked as answer by Jay_P85 Sunday, February 01, 2015 8:40 PM
    Monday, January 26, 2015 7:19 PM

All replies

  • which line do you get the exception? dont think it goes wwrong with deserialisation but something else in your code looks broken

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, January 26, 2015 9:37 AM
  • When i try to Debug write the values in item.cellno.CellC and item.cellno.Voda
    Monday, January 26, 2015 9:46 AM
  • in your model its just a property. in your json its a list. you have this:

     "CellNo": [
               
    {
                   
    "CellC": "0791408989",
                   
    "Voda": "0117689009"
               
    }
           
    ]

    and should be

     "CellNo": 
               
    {
                   
    "CellC": "0791408989",
                   
    "Voda": "0117689009"
               
    }
           


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, January 26, 2015 9:49 AM
  • Hi, I'm still getting 'System.NullReferenceException' occurred error.
    Monday, January 26, 2015 9:55 AM
  • you should make name the same (also capitals)

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, January 26, 2015 11:37 AM
  • The problem that i'm having is accessing the CellNo is giving me null reference error.
    Monday, January 26, 2015 5:20 PM
  • I have solved it 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

    • Marked as answer by Jay_P85 Sunday, February 01, 2015 8:40 PM
    Monday, January 26, 2015 7:19 PM