I'm trying to deserialize a json returned from a query. Essentially my json have this structure:
{"monday":{"start":"13:21","end":"13:21","breaks":[{"start":"13:21","end":"13:21"},{"start":"13:21","end":"13:21"}]},"tuesday":{"start":null,"end":null,"breaks":[]},"wednesday":{"start":"13:21","end":"13:21","breaks":[{"start":"13:21","end":"13:21"}]},"thursday":{"start":null,"end":null,"breaks":[]},"friday":{"start":null,"end":null,"breaks":[]},"saturday":{"start":null,"end":null,"breaks":[]},"sunday":{"start":"13:21","end":"13:21","breaks":[{"start":"13:21","end":"13:21"},{"start":"13:21","end":"13:21"},{"start":"13:21","end":"13:21"}]}}
Now for deserialize the json I use Newtonsoft.Json library for .Net
I pass the json to this function as parameter:
Sub Working_Plan_Deserialize(ByVal wp As String)
Dim working_plan = JsonConvert.DeserializeObject(Of WorkWeek)(wp)
End Sub
I get this result:
Razor.Users+WorkWeek
Now the WorkWeek class is declared as follow:
Public Class WorkDay
<JsonProperty("start")>
Public Property starttime As String
<JsonProperty("end")>
Public Property endtime As String
Public Property breaks As New List(Of Break)
End Class
Public Class Break
<JsonProperty("start")>
Public Property starttime As String
<JsonProperty("end")>
Public Property endtime As String
End Class
Public Class WorkWeek
Public Property monday As WorkDay
Public Property tuesday As WorkDay
Public Property wednesday As WorkDay
Public Property thursday As WorkDay
Public Property friday As WorkDay
Public Property saturday As WorkDay
Public Property sunday As WorkDay
Public Sub New()
monday = New WorkDay
tuesday = New WorkDay
wednesday = New WorkDay
thursday = New WorkDay
friday = New WorkDay
saturday = New WorkDay
sunday = New WorkDay
End Sub
End Class
What I'm trying to do is iterate on the object returned, that's have this structure:

How you can see I've the week days, so I want access to each days and get the property as break or the start and end time. How I can achieve this? Actually the for each need a specific day in the iteration and I can't have 7 loop for each day.
Your WorkWeek object is a flat object of all the days. It is not a collection, so you cant iterate it. You can deserialize to the Dictionary form though:
Dim result = JsonConvert.DeserializeObject(Of Dictionary(Of String, WorkDay))(jstr)
However, the order of Dictionaries is unspecified so to iterate in order, you need something like:
Private DayNames As String() = {"sunday", "monday"...}
Then to iterate the Dictionary:
For Each day As String in DayNames
Dim thisDay = result(day)
thisDay would be a WorkDay object for the day specified.
Finally, the debug display is not very informative, but there is not much to report on them. If you click the arrows, it should display the data for each WorkDay object. You could override ToString() but there is nothing distinctive to report in those WorkDay objects: they do not even know what day they are for.
Some of this is driven by the fact that you started with a very specific json layout and drove the VB app from that. Put together otherwise it might have been easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With