Serialize jvb.net list to json

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

I need to serialize vb.net code to json.

I need the json to look like this:
{
    "KalTid": [
        {
            "AnstNr": "1",
            "Datum": "2014-01-02",
            "RadId": "9b8412eb-77a4-4e0b-853e-fdd78edeac8a",
            "UseTid": true,
            "StartTid": "08:00",
            "SlutTid": "08:00",
            "Timmar": 8,
            "Dagar": 0,
            "TidKod": "SJK",
            "UseBrukare": true,
            "UseResEnhet1": false,
            "UseResEnhet2": false,
            "UseResEnhet3": false,
            "Brukare": "1",
            "ResEnhet1": "",
            "ResEnhet2": "",
            "ResEnhet3": "",
            "Registrerad": true,
            "CalcTyp": "",
            "Source": "PORTAL"
        }
    ]
}

Open in new window


The vb.net classes I'm using are looking like this:
Imports Microsoft.VisualBasic
Imports Newtonsoft.Json

Public Class KalTid
    <JsonProperty("AnstNr")>
    Public Property AnstNr As Long

    <JsonProperty("Datum")>
    Public Property Datum As DateTimeOffset

    <JsonProperty("RadId")>
    Public Property RadId As Guid

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

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

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

    <JsonProperty("Timmar")>
    Public Property Timmar As Long

    <JsonProperty("Dagar")>
    Public Property Dagar As Long

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

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

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

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

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

    <JsonProperty("Brukare")>
    Public Property Brukare As Long

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

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

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

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

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

    <JsonProperty("Source")>
    Public Property Source As String
End Class

Public Class CronaTime
    <JsonProperty("KalTid")>
    Public Property KalTid As List(Of KalTid)
End Class

Open in new window


I'm using this code to add to the list and then serialize it:
 Dim list As New List(Of KalTid)
        Dim item = New KalTid
        item.AnstNr = "1"
        item.Datum = "2018-09-05"
        item.RadId = New Guid
        item.UseTid = False
        item.StartTid = "08:00"
        item.SlutTid = "17:00"
        item.Timmar = 8
        item.TidKod = "ARB"
        list.Add(item)

        Dim json = JsonConvert.SerializeObject(list, Formatting.None)

Open in new window


The output I'm getting is this:
[{"AnstNr":1,"Datum":"2018-09-05T00:00:00+02:00","RadId":"00000000-0000-0000-0000-000000000000","UseTid":false,"StartTid":"08:00","SlutTid":"17:00","Timmar":8,"Dagar":0,"TidKod":"ARB","UseBrukare":false,"UseResEnhet1":false,"UseResEnhet2":false,"UseResEnhet3":false,"Brukare":0,"ResEnhet1":null,"ResEnhet2":null,"ResEnhet3":null,"Registrerad":false,"CalcTyp":null,"Source":null}]

Open in new window


How can I get the first paragrapgh (KalTid) to begin the json?

Thanks for help!

Peter
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
JSON is only for serializing data, not code.

And for your problem: You need a container. E.g.

Imports Newtonsoft.Json

Public Class KalTid
    <JsonProperty("AnstNr")>
    Public Property AnstNr As Long
    <JsonProperty("Datum")>
    Public Property Datum As DateTimeOffset
    <JsonProperty("RadId")>
    Public Property RadId As Guid
    '[..]
    <JsonProperty("Source")>
    Public Property Source As String
End Class

Public Class Container
    <JsonProperty("KalTid")>
    Public Items As New List(Of KalTid)
End Class

Module Module1
    Sub Main(args As String())
        Dim container As New Container
        Dim item As New KalTid
        item.AnstNr = "1"
        item.Datum = "2018-09-05"
        item.RadId = New Guid
        item.Source = "SOURCE"
        container.Items.Add(item)

        Dim json As String = JsonConvert.SerializeObject(container, Formatting.None)
        Console.WriteLine(json)

        Console.WriteLine("Done.")
        Console.ReadLine()
    End Sub
End Module

Open in new window

Peter NordbergIT Manager

Author

Commented:
Thanks!

Worked fine!

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