当Json具有多个级别时,将Json转换为Datatable
在我的程序中,这是我第一次尝试解析JSON内容。当Json具有多个级别时,将Json转换为Datatable,json,vb.net,datatable,json.net,deserialization,Json,Vb.net,Datatable,Json.net,Deserialization,在我的程序中,这是我第一次尝试解析JSON内容。 我试图得到的结果是一个包含如下列的DataTable: | Text of group | Text of Item | Command of Item | JSON如下所示: { "Groups": [ { "Items": [ { "Command": "Frame
我试图得到的结果是一个包含如下列的DataTable:
| Text of group | Text of Item | Command of Item |
JSON如下所示:
{
"Groups": [
{
"Items": [
{
"Command": "Framework.Windows.Components.ESScrollerForm, ESGrid|SHOW|Χρήστες|ESGOUser|ESGOUser_def|||65535",
"Key": "834888dd-c4d5-449a-96b7-67db5c3d2692",
"Text": "Users",
"ImageIndex": -1
},
{
"Command": "Framework.Windows.Components.ESScrollerForm, ESGrid|SHOW|QuestionaireSurveyorQuery|ESTMTask|QuestionaireSurveyorQuery|||0",
"Key": "b71de66d-2baf-4452-ada7-8fc67044876b",
"Text": "QuestionaireSurveyorQuery"
}
],
"Expanded": true,
"Tag": "",
"Key": "b741e67a-a3cd-4b97-91cf-ae9c9d9db7d7",
"Text": "Settings",
"ImageIndex": -1
},
{
"Items": [
{
"64String": "Soap",
"Command": "cInvoke|Booked Requests Agent Booking|SHOW|ESMIS|BookedReqAgentBook||False",
"Key": "bfbc3d4a-ef8a-49a0-918a-331813ba90fb",
"Text": "Requests Agent Booking",
"ImageIndex": -1
},
{
"64String": "Jrse",
"Command": "cInvoke|HHG SF Profit \u0026 Loss|SHOW|ESMIS|HHGFileProfitability||False",
"Key": "cf1cbffc-aba9-4e0f-8d6c-ba7219932fb6",
"Text": "HHG SF Profit \u0026\u0026 Loss",
"ImageIndex": -1
}
],
"Tag": "..CSShortcuts\\HHGReporting.ebl",
"Key": "eff0d713-a70e-4582-a103-b8cc5cecdad6",
"Text": "HHGReporting",
"ImageIndex": -1
}
]
}
过去,我使用XPATH成功地解析了复杂的XML,但现在我在使用Newtonsoft.Json时遇到了很多困难,但没有成功
我尝试的是首先使用在线生成器创建3个类:
Public Class Rootobject
Public Property Groups() As Group
End Class
Public Class Group
Public Property Items() As Item
Public Property Expanded As Boolean
Public Property Tag As String
Public Property Key As String
Public Property Text As String
Public Property ImageIndex As Integer
End Class
Public Class Item
Public Property Command As String
Public Property Key As String
Public Property Text As String
Public Property ImageIndex As Integer
Public Property 64String As String
End Class
如果您想知道反序列化命令应该是什么样的,以便将数据作为具有所述结构的DataTable获取?如果您有一个几乎是工作类的模型,则需要进行一些更改以使其按预期工作 这种语法具有误导性:
Public Property Groups() As Group
这不是对象的集合,它只是一个类型为Group
的单个对象 将所有更改为:
Public Property Groups As Group()
'or
Public Property Groups As List(Of Group)
要转换为具有特定列选择的DataTable,您需要迭代Groups集合,对于每个组,迭代Items集合,以提取所需的值。这里,我使用一个专门的类,
GroupsHandler
,它包含用于反序列化兼容JSON并将结果数据结构的部分内容转换为DataTable的类模型
GroupsHandler
类公开了两个公共方法:
,用于将JSON内容转换为.Net类反序列化()
,用于从反序列化内容创建数据表ToDataTable()
Dim handler = New GroupsHandler(Json)
' Only deserialize
Dim myGroups = handler.Deserialize()
' Deserialize and convert to DataTable
Dim dt = handler.ToDataTable()
Group
类名在ItemsGroup
中更改,因为Group
是一个语言关键字。64String
属性名称在String64
中更改,因为属性名称不能以数字开头
导入Newtonsoft.Json
公共类GroupsHandler
作为GroupsRoot的私有根=无
私有m_json As String=String.Empty
Public Sub New(json作为字符串)
m_json=json
端接头
公共函数反序列化()为(ItemsGroup的)列表
root=JsonConvert.DeserializeObject(属于GroupsRoot)(m_json)
返回root.Groups
端函数
作为DataTable的公共函数ToDataTable()
如果根什么都不是,那么
如果String.IsNullOrEmpty(m_json),则不返回任何内容
反序列化()
如果结束
Dim dt作为新数据表(“组”)
dt.Columns.AddRange(新数据列(){
新数据列(“GroupText”,GetType(String)),
新数据列(“ItemText”,GetType(String)),
新数据列(“ItemCommand”,GetType(String))
})
对于root.Groups中的每个grp
对于grp.Items中的每个项目
添加(新对象(){grp.Text,item.Text,item.Command})
下一个
下一个
返回dt
端函数
公共类GroupsRoot
作为列表的公共属性组(项目组)
末级
公共类项目组
作为列表的公共财产项目(项目)
公共属性扩展为布尔值?
公共属性标记为字符串
作为Guid的公共属性密钥
公共属性文本作为字符串
公共财产索引
末级
公共类项目
作为字符串的公共属性命令
作为Guid的公共属性密钥
公共属性文本作为字符串
公共财产指数有多长?
公共属性String64作为字符串
末级
末级
Imports Newtonsoft.Json
Public Class GroupsHandler
Private root As GroupsRoot = Nothing
Private m_json As String = String.Empty
Public Sub New(json As String)
m_json = json
End Sub
Public Function Deserialize() As List(Of ItemsGroup)
root = JsonConvert.DeserializeObject(Of GroupsRoot)(m_json)
Return root.Groups
End Function
Public Function ToDataTable() As DataTable
If root Is Nothing Then
If String.IsNullOrEmpty(m_json) Then Return Nothing
Deserialize()
End If
Dim dt As New DataTable("Groups")
dt.Columns.AddRange(New DataColumn() {
New DataColumn("GroupText", GetType(String)),
New DataColumn("ItemText", GetType(String)),
New DataColumn("ItemCommand", GetType(String))
})
For Each grp In root.Groups
For Each item In grp.Items
dt.Rows.Add(New Object() {grp.Text, item.Text, item.Command})
Next
Next
Return dt
End Function
Public Class GroupsRoot
Public Property Groups As List(Of ItemsGroup)
End Class
Public Class ItemsGroup
Public Property Items As List(Of Item)
<JsonProperty("Expanded", NullValueHandling:=NullValueHandling.Ignore)>
Public Property Expanded As Boolean?
Public Property Tag As String
Public Property Key As Guid
Public Property Text As String
Public Property ImageIndex As Long
End Class
Public Class Item
Public Property Command As String
Public Property Key As Guid
Public Property Text As String
<JsonProperty("ImageIndex", NullValueHandling:=NullValueHandling.Ignore)>
Public Property ImageIndex As Long?
<JsonProperty("64String", NullValueHandling:=NullValueHandling.Ignore)>
Public Property String64 As String
End Class
End Class