问题描述
我对 vb.net 有点生疏,我需要你的帮助来编码:
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"}]}}
在 vb.net 中的 json 格式中,您如何看到这个字符串已经在 json 中编码,但我想知道如何创建一个数组或类似的东西来存储休息时间的日子.本质上:
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?
更新:
workWeek.saturday.starttime = User.saturday_start.Value
workWeek.saturday.endtime = User.saturday_end.Value
你怎么能看到我将一个名为 saturday_start
和 saturday_end
的 DateTimePicker 正确开始和结束日期传递给 saturday
工作日的类变量>.
现在根据你的提示,我想知道输入这些数据后,我如何在json中编码?
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?
更新 #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
推荐答案
end
在 VB 中将是非法的属性名称,break
可能会阻塞 C#.所以这使用了 JSON.NET,它允许您轻松更改属性名称:
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)
迭代:
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
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.
要从 VB 对象创建该 json,只需将其序列化:
To create that json from the VB objects, just serialize it:
Dim jstr = JsonConvert.SerializeObject(myWkWeek)
这篇关于如何在json中正确转换数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!