Serialize jvb.net list to json

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
Peter NordbergIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter NordbergIT ManagerAuthor Commented:
Thanks!

Worked fine!

Peter
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.