我可以序列化JSON的主树(frm);但是,我不知道如何为第二个标题为控件的树分配值。
要为主树赋值,我可以这样做:
Dim form As New frm()
form.form_name = "FORM"
我希望能够分配这样的控件:
form.controls.parent = "something"
我是否以错误的方式完成作业,或者我的班级设置不正确?下面我将展示序列化信息的代码,然后展示 jSON 文件的样子,然后展示 JSON 文件的类。如有任何帮助,我们将不胜感激。
代码:
Dim form As New frm()
form.form_name = "name"
form.form_text = "text"
form.form_icon = "icon"
form.form_default_button = "default"
form.form_cancel_button = "cancel"
form.form_width = "width"
form.form_height = "height"
Dim JSON As String = JsonConvert.SerializeObject(form)
JSON 示例:
{
"form_name":"FORM",
"form_text":"Setup Buyers PO Issue Limit",
"form_icon":"c:\test.ico",
"form_default_button":"BTNSAVE",
"form_cancel_button":"BTNCLOSE",
"form_width":"300",
"form_height":"800",
"controls":[
{
"parent":"FORM",
"type":"LABEL",
"name":"LBLUser",
"text":"Buyers Operator ID:",
"align":"LEFT",
"enabled":"TRUE",
"mandatory":"",
"validator":"",
"populate":""
},
{
"parent":"FORM",
"type":"COMBO",
"name":"CBOOperator",
"text":"",
"align":"LEFT",
"tooltip":"",
"enabled":"TRUE",
"mandatory":"TRUE",
"validator":"",
"populate":"SUB"
}
]
}
类别代码:
Public Class controls
Public Property parent() As String
Get
Return m_parent
End Get
Set
m_parent = Value
End Set
End Property
Private m_parent As String
Public Property type() As String
Get
Return m_type
End Get
Set
m_type = Value
End Set
End Property
Private m_type As String
Public Property name() As String
Get
Return m_name
End Get
Set
m_name = Value
End Set
End Property
Private m_name As String
Public Property text() As String
Get
Return m_text
End Get
Set
m_text = Value
End Set
End Property
Private m_text As String
Public Property align() As String
Get
Return m_align
End Get
Set
m_align = Value
End Set
End Property
Private m_align As String
Public Property enabled() As Boolean
Get
Return m_enabled
End Get
Set
m_enabled = Value
End Set
End Property
Private m_enabled As Boolean
Public Property mandatory() As Boolean
Get
Return m_mandatory
End Get
Set
m_mandatory = Value
End Set
End Property
Private m_mandatory As Boolean
Public Property validator() As String
Get
Return m_validator
End Get
Set
m_validator = Value
End Set
End Property
Private m_validator As String
Public Property populate() As String
Get
Return m_populate
End Get
Set
m_populate = Value
End Set
End Property
Private m_populate As String
Public Property tooltip() As String
Get
Return m_tooltip
End Get
Set
m_tooltip = Value
End Set
End Property
Private m_tooltip As String
End Class
Public Class frm
Public Property form_name() As String
Get
Return m_form_name
End Get
Set
m_form_name = Value
End Set
End Property
Private m_form_name As String
Public Property form_text() As String
Get
Return m_form_text
End Get
Set
m_form_text = Value
End Set
End Property
Private m_form_text As String
Public Property form_icon() As String
Get
Return m_form_icon
End Get
Set
m_form_icon = Value
End Set
End Property
Private m_form_icon As String
Public Property form_default_button() As String
Get
Return m_form_default_button
End Get
Set
m_form_default_button = Value
End Set
End Property
Private m_form_default_button As String
Public Property form_cancel_button() As String
Get
Return m_form_cancel_button
End Get
Set
m_form_cancel_button = Value
End Set
End Property
Private m_form_cancel_button As String
Public Property form_width() As String
Get
Return m_form_width
End Get
Set
m_form_width = Value
End Set
End Property
Private m_form_width As String
Public Property form_height() As String
Get
Return m_form_height
End Get
Set
m_form_height = Value
End Set
End Property
Private m_form_height As String
Public Property controls() As Control()
Get
Return m_controls
End Get
Set
m_controls = Value
End Set
End Property
Private m_controls As Control()
End Class
序列化对象最简单的方法是首先将类设为“POCO”,例如:
Public Class DemoClass
Public Property Property1 As String
Public Property Property2 As Decimal
Public Property Property3 As Integer
Public Property Property4 As Boolean
End Class
然后导入
Json
命名空间:
Imports System.Text.Json
最后使用
JsonSerializer
进行序列化:
Dim c1 As New DemoClass With {.Property1 = "Property1Value", .Property2 = 12.34D, .Property3 = 5, .Property4 = True}
Dim s = JsonSerializer.Serialize(c1)
System.IO.File.WriteAllText("C:\Junk\Junk.json", s)