我对vb.net有点生疏,我需要你的帮助来编码:
{"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"}]}}
在vb.net中以json格式,如何看到这个字符串已经在json中编码了但是我想知道如何创建一个数组或类似的东西来存储休息时间的日子。基本上:
"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"
}
]
},
我想创建这种类型的内容最好的想法应该是使用字典 有人能够以恰当而快速的方式获得这个结果吗?
更新
workWeek.saturday.starttime = User.saturday_start.Value
workWeek.saturday.endtime = User.saturday_end.Value
如何看待我从名为saturday
和saturday_start
的DateTimePicker传递给saturday_end
工作日的类变量正确的开始和结束日期。
现在从你的提示中,我想知道输入这些数据后,如何在json中编码?
更新#2 - 添加休息
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
答案 0 :(得分:1)
end
将是VB中的非法属性名称,break
可能会阻塞C#。因此,这使用JSON.NET,它允许您轻松更改属性名称:
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
反序列化为工作日名称为键的字典:
Dim jstr = ...from whereever
Dim result = JsonConvert.DeserializeObject(Of Dictionary(Of String, WorkDay))(jstr)
迭代:
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
输出:
日:星期一开始:09:00结束:18:00
日:星期二开始:09:00结束:18:00
日:周三开始:09:00结束:18:00
等他们都是一样的
I actually thought also to create an object that has the whole week
如果您想要一个扁平物体,请添加以下类:
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)
对我来说似乎有点多余,但可能更简单,具体取决于它是什么以及如何使用它。
要从VB对象创建json,只需序列化它:
Dim jstr = JsonConvert.SerializeObject(myWkWeek)