1

I'm a bit rusty with vb.net and I need your help for encoding this:

{"monday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"tuesday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"wednesday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"thursday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"friday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"saturday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"sunday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]}}

in json format in vb.net, how you can see this string is already encoded in json but I want know how I can create an array or something like for store the days with the break time. Essentially:

"monday":{
"start":"09:00",
"end":"18:00",
"breaks":[
{
"start":"11:20",
"end":"11:30"
},
{
"start":"14:30",
"end":"15:00"
}
]
},
"tuesday":{
"start":"09:00",
"end":"18:00",
"breaks":[
{
"start":"11:20",
"end":"11:30"
},
{
"start":"14:30",
"end":"15:00"
}
]
},

I guess for create this type of content the best idea should be to use a dictionary.
Someone could show a properly and fast way to get this result?

UPDATE:

workWeek.saturday.starttime = User.saturday_start.Value
workWeek.saturday.endtime = User.saturday_end.Value

How you can see I pass to the class variable for saturday working day the properly start and end date from a DateTimePicker called saturday_start and saturday_end.
Now from your hint, I want to know after these data have been entered, how can I encode in json?

UPDATE #2 - Adding breaks

 For Each row As DataGridViewRow In User.break_view.Rows

                If Not row.IsNewRow Then

                    Console.WriteLine(row.Cells(0).Value.ToString)

                    If row.Cells(0).Value.ToString = "Monday" Then

                        workWeek.monday.breaks.starttime = row.Cells(1).Value.ToString ' <- This is also wrong I guess, it's not this the way for add the break to a specific day?

                    End If

                   ... other day condition ..

                End If

            Next
Dillinger
  • 1,823
  • 4
  • 33
  • 78
  • Json is not a format. It is serialized object data - that is, it is the text notation of a number of Class property-values, so the layout or resulting format is that of the class objects which were serialized. Is the point to create objects which represent that structure? Deserializing that json or what? – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 15:45
  • I actually thought also to create an object that has the whole week. My aim is to achieve the result as shown in my question. – Dillinger Dec 13 '15 at 15:47
  • But `help for encoding this:` - that json is already encoded! – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 15:52
  • I know, I've encoded this string in PHP when the user fill some information, in this case the working plan of the provider. The problem's that I don't know how I can get this structure in vb.net. The goal is take the same structure for perform a db sync between client and web app. – Dillinger Dec 13 '15 at 15:54
  • Can you change the encoding? Because some of the names are illegal – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 15:57
  • which names? What do you mean when said "illegal"? Maybe already use from vb.net itself? But yes I can change the name if could cause problem, I just want achieve this format for insert in the db and sync in online db. That's it. – Dillinger Dec 13 '15 at 16:00
  • This goes back to the very first question: *what* are trying to do: READ that json or CREATE that json. My answer shows how to read it (based on that you said `I've encoded this string in PHP`). So I thought the json was the starting point. If you want to create the json you would use `.SerializeObject` rather than `DEserializeObject`. You also need to turn on Option Strict - a DTP returns a DateTime type, you are assigning it to a string. Many Bad Things happen using the wrong Type – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 17:02
  • Okay, sorry for the misunderstanding. – Dillinger Dec 13 '15 at 17:11
  • Keep in mind we know almost nothing about whatever you are working on. All we know is what you *think* we need to know. I updated the answer to show how to serialize (not encode) a `WorkWeek` object to json. – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 17:15
  • I don't know why but when I perform: workWeek.monday.starttime = User.monday_start.Value I get NullReference Exception. I check and the monday_start.Value return correctly the value, what I missing? Maybe option strict? – Dillinger Dec 13 '15 at 17:23
  • I have no context for what you are doing now or where. `WorkWeek` is a Type so you should use a *different* name for your variables. `WorkWeek.Monday` is also a type, so if you are starting off "fresh" in VB you need to use the `New` keyword to create a new `WorkWeek` object. I will add a ctor to the answer for the others since now I know you are creating these (deserializing JSON does it for you). – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 17:27
  • I've just create the class constructor as Dim workWeek As New WorkWeek and then, workWeek.monday.starttime = User.monday_start.Value for add the value of each day but it return NullReferenceException. The declaration of this instance is inside of the population function, and of course I've added new – Dillinger Dec 13 '15 at 17:35
  • A) VB is not case sensitive so `workWeek` is still the same as `WorkWeek` B) when you get the NRE hold the mouse over the different variables and it will tell you which one is `Nothing` see also http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it?rq=1 which is all about the NRE. Also, the WorkWeek in the answer now has a ctor, and Breaks changed too because it needs to be initialized too – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 17:38
  • workWeek return all week days as Nothing, check my image – Dillinger Dec 13 '15 at 17:46
  • 1
    did you add the `Sub New` from the answer edit...for all 7 days? I rolled back your edit - you dont want a NRE ref or it will get closed as a dupe. use pastebin for the relevant code if you want – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 17:48
  • Okay now seems that all working fine. Thank you very much. If I bother you last time, I want add the breaks for each days. Now all breaks are added in a DataGridView, how you can see I can have multiple breaks for each day. Now I know how iterate on the DataGridView (I've update my code) with this iteration, but if you could tell me if there's a fast way to add the breaks for each day, actually my idea is to compare each day with the cell name actually in the iteration and if the day are equal to the compared string I insert the break for this day, I've 7 condition so it's not a good way I gs. – Dillinger Dec 13 '15 at 18:19
  • We are now 3 issues removed from the original. Just post a new question - comments dont allow enough detail – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 18:22
  • You're right at this point I try myself and if I can't do this I'll open new question, thank you anyway for this amazing help. – Dillinger Dec 13 '15 at 18:23
  • `breaks` is an array or list (I have no idea what you used) you have to add NEW Break objects to it – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 18:29

1 Answers1

1

end would be an illegal property name in VB, break might choke C#. So this uses JSON.NET which allows you to change property names easily:

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

Deserialize to a Dictionary where the weekday names are the keys:

Dim jstr = ...from whereever

Dim result = JsonConvert.DeserializeObject(Of Dictionary(Of String, WorkDay))(jstr)

To iterate:

For Each kvp As KeyValuePair(Of String, WorkDay) In result
    Console.WriteLine("Day: {0}  start: {1}  end: {2}",
                      kvp.Key, kvp.Value.starttime, kvp.Value.endtime)
Next

Output:

Day: monday start: 09:00 end: 18:00
Day: tuesday start: 09:00 end: 18:00
Day: wednesday start: 09:00 end: 18:00

etc they are all the same

I actually thought also to create an object that has the whole week If you want a flat object, add this 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

    ' a ctor to initialize the workday objects when
    ' starting from VB:
    Public Sub New
        monday = New WorkDay
        tuesday = New WorkDay
        ' etc...
    End Sub
End Class

' deserialize:
Dim myWkWeek = JsonConvert.DeserializeObject(Of WorkWeek)(jstr)

It seems a bit redundant to me, but could be simpler depending on what it is and how it is used.


To create that json from the VB objects, just serialize it:

Dim jstr = JsonConvert.SerializeObject(myWkWeek)
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Okay seems good, but how I can populate the jstr? I mean how I can add for example to monday start and end time and add also multiples break? Thanks for the support. – Dillinger Dec 13 '15 at 16:14
  • 1
    The `WorkDay` type (object) has a `Breaks` array where those start/stops are located. If you want to add/edit `Breaks` change Workday to: `Public Property breaks As List(Of Break)`. It is much easier to add/remove from `List` than old fashioned arrays – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 16:16
  • I almost always use `List` in place of an array, but your question *specifically* asks for an array. "populate the jstr" would be serializing a dictionary or WorkWeek object (and *thats* what I was asking about - it is not clear whether you are trying to READ the json or CREATE the json from VB) – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 16:19
  • It's no a problem I asked for an array in vb.net because I thought it would be easier if it is a problem for you, do not hesitate to use a list. Another question, I don't understand how I can add start and end to a particular day. I've used monday.start = "" but I can' see any monday variable – Dillinger Dec 13 '15 at 16:24
  • 1
    `myWkWeek.Monday.starttime =...` or if you are using the dictionary method: `result ("monday").starttime = ...` it will be `endtime` in VB, `"end"` in json for the reasons mentioned – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 16:27
  • Only a things I don't understand you have passed jstr but how I can valorize it? Could you post an example please? Thank you – Dillinger Dec 13 '15 at 16:49
  • can you reword please, valorize? create it? validate it? – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 16:51
  • No, I do not do chat, sorry. I dont know what you are asking – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 16:53
  • Oh okay, so Dim jstr = ...from whereever, what is whereever? – Dillinger Dec 13 '15 at 16:54
  • Well, ***I*** just loaded it from a text file - you are getting it from whereever your php thing is located. I have no idea of how you are exchanging data – Ňɏssa Pøngjǣrdenlarp Dec 13 '15 at 16:56
  • In the vb.net app the information are filled simply from a DataTimePicker, check my update. – Dillinger Dec 13 '15 at 16:57
  • when I deserialize the json I've a list of days, how I can iterate over it? Could you show me an example? Thanks. – Dillinger Dec 14 '15 at 13:09
  • Since you chose the flat method (WorkWeek vs Dictionary) you cant iterate per se. Reference the data the same in the deserialized object: `working_plan.monday.starttime` or `working_plan.monday.breaks(0).starttime` – Ňɏssa Pøngjǣrdenlarp Dec 14 '15 at 13:15