0

I did this to convert JSON formatted string to VB.NET Object.

I cannot find why it does not work!

jsonSettingsWrapper.vb

Public Class jsonSettingsWrapper
    'Public setting As jsonSettings
    Public Property setting As jsonSettings
        Get
            Return m_setting
        End Get
        Set(value As jsonSettings)
            m_setting = value
        End Set
    End Property
    Private m_setting As jsonSettings
End Class

Public Class jsonSettings
    Public Property root_folder_path As String
        Get
            Return m_root_folder_path
        End Get
        Set(value As String)
            m_root_folder_path = value
        End Set
    End Property
    Private m_root_folder_path As String

    Public Property ignored_files As String()
        Get
            Return m_ignored_files
        End Get
        Set(value As String())
            m_ignored_files = value
        End Set
    End Property
    Private m_ignored_files As String()

    Public Property last_pack_date As String
        Get
            Return m_last_pack_date
        End Get
        Set(value As String)
            m_last_pack_date = value
        End Set
    End Property
    Private m_last_pack_date As String

    Public Property last_push_date As String
        Get
            Return m_last_push_date
        End Get
        Set(value As String)
            m_last_push_date = value
        End Set
    End Property
    Private m_last_push_date As String

    Public Property no_of_packs As Integer
        Get
            Return m_no_of_packs
        End Get
        Set(value As Integer)
            m_no_of_packs = value
        End Set
    End Property
    Private m_no_of_packs As Integer

    Public Property packs_array As packs()
        Get
            Return m_packs_array
        End Get
        Set(value As packs())
            m_packs_array = value
        End Set
    End Property
    Private m_packs_array As packs()
End Class

Public Class packs
    Public Property filename As String
        Get
            Return m_filename
        End Get
        Set(value As String)
            m_filename = value
        End Set
    End Property
    Private m_filename As String

    Public Property pack_date As String
        Get
            Return m_pack_date
        End Get
        Set(value As String)
            m_pack_date = value
        End Set
    End Property
    Private m_pack_date As String
End Class

Form1.Vb

Imports System.Web.Script.Serialization
Imports WindowsApplication1.jsonSettingsWrapper

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ser As New JavaScriptSerializer
        Dim settingsString As String = IO.File.ReadAllText("settings.json")
        MsgBox(settingsString)
        Dim settings = ser.Deserialize(Of jsonSettingsWrapper)(settingsString)
        MsgBox(settings.setting) 'empty
        MsgBox(settings.setting.root_folder_path) 
        ' ^ ^ Error: A first chance exception of type 'System.NullReferenceException' occurred in FilesSync.exe
    End Sub

As mentioned in the code comments, error occur when it try to access some of its value. Can anyone put some light on why is this happening. Where am i doing it wrong?

EDIT: The JSON I used is:

{"settings": {
    "root_folder_path": "ab",
    "ignored_files": [
        "cd",
        "ef"
    ],
    "last_pack_date": "12",
    "last_push_date": "21",
    "no_of_packs": 3,
    "packs_array": [
        {
            "filename": "aa",
            "pack_date": "21"
        }
    ]
}}
cipher
  • 2,414
  • 4
  • 30
  • 54

1 Answers1

0

Your JSON use the property name "settings" but your class use "setting", change your class to use the right name

Public Class jsonSettingsWrapper
    'Public setting As jsonSettings
    Public Property settings() As jsonSettings
        Get
            Return m_setting
        End Get
        Set(ByVal value As jsonSettings)
            m_setting = value
        End Set
    End Property
    Private m_setting As jsonSettings
End Class

or use DataContract

Community
  • 1
  • 1
the_lotus
  • 12,668
  • 3
  • 36
  • 53