问题描述
我正在尝试反序列化从查询返回的 json.基本上我的 json 有这个结构:
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"}]}}
现在反序列化 json 我使用 Newtonsoft.Json
库用于 .Net
我将json作为参数传递给这个函数:
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
我得到了这个结果:
Razor.Users+WorkWeek
现在 WorkWeek
类声明如下:
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:
你怎么能看到我有工作日,所以我想访问每一天并将属性作为休息时间或开始和结束时间.我怎么能做到这一点?实际上 for each 在迭代中需要特定的一天,我不能每天有 7 个循环.
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.
推荐答案
您的 WorkWeek
对象一直都是平面对象.它不是一个集合,所以你不能迭代它.您可以反序列化为 Dictionary 形式:
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"...}
然后迭代字典:
For Each day As String in DayNames
Dim thisDay = result(day)
thisDay
将是指定日期的 WorkDay
对象.
thisDay
would be a WorkDay
object for the day specified.
最后,调试显示的信息量不是很大,但没有太多可报告的.如果单击箭头,它应该显示每个 WorkDay
对象的数据.您可以覆盖 ToString()
但在这些 WorkDay 对象中没有什么特别的报告:他们甚至不知道他们是哪一天.
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.
其中一些原因是您从一个非常具体的 json 布局开始,并由此驱动了 VB 应用程序.否则可能会更容易.
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.
这篇关于如何迭代json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!