Right representation of vb class for json string

Peter Nordberg
Peter Nordberg used Ask the Experts™
on
Hi,

I have this json request that I want to deserialize to an vb object. The json looks like this:
{
    "PerReg": [
        {
            "AnstNr": "1",
            "EfNamn": "Ljunggren",
            "FoNamn": "Siv",
            "Adress1": "Föreningsgatan 33",
            "Adress2": "",
            "PostNr": "447 34",
            "PostOrt": "VÅRGÅRDA",
            "LandAdr": "",
            "EpostPriv": "",
            "EpostArb": "",
            "Telefon": "0322-21298",
            "Mobil": "",
            "PersNr": "197901049283",
            "Signatur": "SL",
            "Befattning": "VD",
            "AnstDatum": "2001-09-01",
            "Slutat": false,
            "AvgDatum": "1899-12-30",
            "AnstTom": "1899-12-30",
            "Locked": false,
            "KalSchemaAuto": false,
            "UseOBRegel": false,
            "LockOBRegel": false,
            "OBRegelID": "",
            "UseArbSchema": true,
            "ArbSchema": "1",
            "Avdelning": "10",
            "KalUseAttest": true,
            "PerTyp": "LED",
            "AnstForm": "TV",
            "LoneForm": "MÅN",
            "LSSBerPerStart": "1899-12-30",
            "LSSBerPerInt": "",
            "LSSAnnanArbGiv": false,
            "LSSArbGivNamn": "",
            "LSSArbGivOrgnr": "",
            "UseSysGrad": true,
            "SysGrad": 100,
            "VeckArbTid": 40,
            "HelVeckTid": 40,
            "VeckArbDgr": 5,
            "DagArbTid": 8,
            "UtbetBank": "",
            "UtbetClearNr": "8052",
            "UtbetKontoNr": "565827416",
            "UseTimLon": false,
            "ManLon": 52000,
            "TimLon": 0,
            "UseResEnhet1": false,
            "UseResEnhet2": false,
            "UseResEnhet3": false,
            "ResEnhet1": "",
            "ResEnhet2": "",
            "ResEnhet3": "",
            "Kategori": "",
            "Befattningskod": "",
            "NYKKod": "",
            "Fritext1": "",
            "Fritext2": "",
            "Fritext3": "",
            "Fritext4": "",
            "Fritext5": "",
            "UtbetPer": "",
            "PrelSkattTyp": "",
            "SkattTab": 0,
            "SkattKol": 0,
            "UseInforsel": false,
            "Inforsel": 0,
            "Forbehall": 0,
            "UseJamkning": false,
            "Jamkning": "",
            "JamBelopp": 0,
            "InkomstGrans": 0,
            "UseSjuklonFri": false,
            "UseSjuklonKar": false,
            "SjuklonFriFrom": "1899-12-30",
            "SjuklonFriTom": "1899-12-30",
            "SemUseFaktor": false,
            "SemBruFaktor": 1
        }
    ]
}

Open in new window


The class looks like this:
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports Newtonsoft.Json

Public Class Employee

#Region "Properties"
    <JsonProperty("AnstNr")>
    Public Property AnstNr As String

    <JsonProperty("EfNamn")>
    Public Property EfNamn As String

    <JsonProperty("FoNamn")>
    Public Property FoNamn As String

    <JsonProperty("Adress1")>
    Public Property Adress1 As String

    <JsonProperty("Adress2")>
    Public Property Adress2 As String

    <JsonProperty("PostNr")>
    Public Property PostNr As String

    <JsonProperty("PostOrt")>
    Public Property PostOrt As String

    <JsonProperty("LandAdr")>
    Public Property LandAdr As String

    <JsonProperty("EpostPriv")>
    Public Property EpostPriv As String

    <JsonProperty("EpostArb")>
    Public Property EpostArb As String

    <JsonProperty("Telefon")>
    Public Property Telefon As String

    <JsonProperty("Mobil")>
    Public Property Mobil As String

    <JsonProperty("PersNr")>
    Public Property PersNr As String

    <JsonProperty("Signatur")>
    Public Property Signatur As String

    <JsonProperty("Befattning")>
    Public Property Befattning As String

    <JsonProperty("AnstDatum")>
    Public Property AnstDatum As String

    <JsonProperty("Slutat")>
    Public Property Slutat As Boolean

    <JsonProperty("AvgDatum")>
    Public Property AvgDatum As String

    <JsonProperty("AnstTom")>
    Public Property AnstTom As String

    <JsonProperty("Locked")>
    Public Property Locked As Boolean

    <JsonProperty("KalSchemaAuto")>
    Public Property KalSchemaAuto As Boolean

    <JsonProperty("UseOBRegel")>
    Public Property UseOBRegel As Boolean

    <JsonProperty("LockOBRegel")>
    Public Property LockOBRegel As Boolean

    <JsonProperty("OBRegelID")>
    Public Property OBRegelID As String

    <JsonProperty("UseArbSchema")>
    Public Property UseArbSchema As Boolean

    <JsonProperty("ArbSchema")>
    Public Property ArbSchema As String

    <JsonProperty("Avdelning")>
    Public Property Avdelning As String

    <JsonProperty("KalUseAttest")>
    Public Property KalUseAttest As Boolean

    <JsonProperty("PerTyp")>
    Public Property PerTyp As String

    <JsonProperty("AnstForm")>
    Public Property AnstForm As String

    <JsonProperty("LoneForm")>
    Public Property LoneForm As String

    <JsonProperty("LSSBerPerStart")>
    Public Property LSSBerPerStart As String

    <JsonProperty("LSSBerPerInt")>
    Public Property LSSBerPerInt As String

    <JsonProperty("LSSAnnanArbGiv")>
    Public Property LSSAnnanArbGiv As Boolean

    <JsonProperty("LSSArbGivNamn")>
    Public Property LSSArbGivNamn As String

    <JsonProperty("LSSArbGivOrgnr")>
    Public Property LSSArbGivOrgnr As String

    <JsonProperty("UseSysGrad")>
    Public Property UseSysGrad As Boolean

    <JsonProperty("SysGrad")>
    Public Property SysGrad As Integer

    <JsonProperty("VeckArbTid")>
    Public Property VeckArbTid As Integer

    <JsonProperty("HelVeckTid")>
    Public Property HelVeckTid As Integer

    <JsonProperty("VeckArbDgr")>
    Public Property VeckArbDgr As Integer

    <JsonProperty("DagArbTid")>
    Public Property DagArbTid As Integer

    <JsonProperty("UtbetBank")>
    Public Property UtbetBank As String

    <JsonProperty("UtbetClearNr")>
    Public Property UtbetClearNr As String

    <JsonProperty("UtbetKontoNr")>
    Public Property UtbetKontoNr As String

    <JsonProperty("UseTimLon")>
    Public Property UseTimLon As Boolean

    <JsonProperty("ManLon")>
    Public Property ManLon As Integer

    <JsonProperty("TimLon")>
    Public Property TimLon As Integer

    <JsonProperty("UseResEnhet1")>
    Public Property UseResEnhet1 As Boolean

    <JsonProperty("UseResEnhet2")>
    Public Property UseResEnhet2 As Boolean

    <JsonProperty("UseResEnhet3")>
    Public Property UseResEnhet3 As Boolean

    <JsonProperty("ResEnhet1")>
    Public Property ResEnhet1 As String

    <JsonProperty("ResEnhet2")>
    Public Property ResEnhet2 As String

    <JsonProperty("ResEnhet3")>
    Public Property ResEnhet3 As String

    <JsonProperty("Kategori")>
    Public Property Kategori As String

    <JsonProperty("Befattningskod")>
    Public Property Befattningskod As String

    <JsonProperty("NYKKod")>
    Public Property NYKKod As String

    <JsonProperty("Fritext1")>
    Public Property Fritext1 As String

    <JsonProperty("Fritext2")>
    Public Property Fritext2 As String

    <JsonProperty("Fritext3")>
    Public Property Fritext3 As String

    <JsonProperty("Fritext4")>
    Public Property Fritext4 As String

    <JsonProperty("Fritext5")>
    Public Property Fritext5 As String

    <JsonProperty("UtbetPer")>
    Public Property UtbetPer As String

    <JsonProperty("PrelSkattTyp")>
    Public Property PrelSkattTyp As String

    <JsonProperty("SkattTab")>
    Public Property SkattTab As Integer

    <JsonProperty("SkattKol")>
    Public Property SkattKol As Integer

    <JsonProperty("UseInforsel")>
    Public Property UseInforsel As Boolean

    <JsonProperty("Inforsel")>
    Public Property Inforsel As Integer

    <JsonProperty("Forbehall")>
    Public Property Forbehall As Integer

    <JsonProperty("UseJamkning")>
    Public Property UseJamkning As Boolean

    <JsonProperty("Jamkning")>
    Public Property Jamkning As String

    <JsonProperty("JamBelopp")>
    Public Property JamBelopp As Integer

    <JsonProperty("InkomstGrans")>
    Public Property InkomstGrans As Integer

    <JsonProperty("UseSjuklonFri")>
    Public Property UseSjuklonFri As Boolean

    <JsonProperty("UseSjuklonKar")>
    Public Property UseSjuklonKar As Boolean

    <JsonProperty("SjuklonFriFrom")>
    Public Property SjuklonFriFrom As String

    <JsonProperty("SjuklonFriTom")>
    Public Property SjuklonFriTom As String

    <JsonProperty("SemUseFaktor")>
    Public Property SemUseFaktor As Boolean

    <JsonProperty("SemBruFaktor")>
    Public Property SemBruFaktor As Integer

#End Region

#Region "Constructor"
    Public Sub New()

    End Sub
#End Region

End Class

Public Class Example
    <JsonProperty("PerReg")>
    Public Property Employee As Employee
End Class

Open in new window


When I try to desierialize I get this error:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Employee' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Open in new window


I guess it has to do with [ ] that is surrounding each PerReg. How do I represent that in the vb class?

Thanks for help!

Peter
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Commented:
Hi,

I think that you need to deserialise the list as it expects an array....

Something like..
JsonConvert.DeserializeObject(Of List(Of Employee))(json)

Public Class Example
    <JsonProperty("PerReg")>
    Public Property Employees As Of List(Of Employee) // Probably need something like this as well...
End Class

Open in new window

.


Hope this helps,

Darren
Peter NordbergIT Manager

Author

Commented:
Thanks!

Worked!

Peter

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial