vb.net - 如何在 VB.NET 中创建 JSON 对象
问题描述
我正在学习通过 vb.net 类创建 json。我知道如何构建 json 数组,但在使用 json 对象时遇到了一些困难。
我所做的是跟随。
- 在 JsonSettings.vb 类中
Public Class JSonSettings
Public Property Yr As List(Of AccYear)
Public Class AccYear
Public Property YearNo As String
Public Property MST As List(Of Master)
Public Property TRN As List(Of Transact)
End Class
Public Class Master
Public Property acList As List(Of AcMaster)
Public Property prList As List(Of PrMaster)
End Class
Public Class PrMaster
Public Property Type As String
Public Property SaleRate As Boolean
End Class
Public Class AcMaster
Public Property Type As String
Public Property PAN As Boolean
End Class
Public Class Transact
Public Property SalList As List(Of SALTR)
Public Property PurList As List(Of PURTR)
Public Property SrtList As List(Of SRTTR)
Public Property PrtList As List(Of PRTTR)
End Class
Public Class Transact1
Public Property SalD As List(Of SALTR)
Public Property PurD As List(Of PURTR)
Public Property SrtD As List(Of SRTTR)
Public Property PrtD As List(Of PRTTR)
End Class
Public Class SALTR
Public Property Type As String
Public Property Lumpsum As Boolean
Public Property Email As Boolean
End Class
Public Class PURTR
Public Property Type As String
Public Property Lumpsum As Boolean
Public Property Email As Boolean
End Class
Public Class SRTTR
Public Property Type As String
Public Property Lumpsum As Boolean
Public Property Email As Boolean
End Class
Public Class PRTTR
Public Property Type As String
Public Property Lumpsum As Boolean
Public Property Email As Boolean
End Class
End Class
在某种形式的按钮点击事件中
Private Sub Settings_Click(sender As Object, e As EventArgs) Handles Settings.Click
Try
Dim st As New JSonSettings
Dim AcmsList As New List(Of JSonSettings.AcMaster)
Dim Acmsitem As New JSonSettings.AcMaster With {
.Type = "AC",
.PAN = True
}
AcmsList.Add(Acmsitem)
'--------------------------------------------------------------------------------------------
Dim PrmsList As New List(Of JsonSettings.PrMaster)
Dim Prmsitem As New JsonSettings.PrMaster With {
.Type = "PR",
.SaleRate = True
}
PrmsList.Add(Prmsitem)
'--------------------------------------------------------------------------------------------
Dim SalLst As New List(Of JsonSettings.SALTR)
Dim Salitem As New JsonSettings.SALTR With {
.Type = "SAL",
.Lumpsum = False,
.Email = False
}
SalLst.Add(Salitem)
'--------------------------------------------------------------------------------------------
Dim PurLst As New List(Of JsonSettings.PURTR)
Dim Puritem As New JsonSettings.PURTR With {
.Type = "PUR",
.Lumpsum = False,
.Email = False
}
PurLst.Add(Puritem)
'--------------------------------------------------------------------------------------------
Dim SrtLst As New List(Of JsonSettings.SRTTR)
Dim Srtitem As New JsonSettings.SRTTR With {
.Type = "SRT",
.Lumpsum = False,
.Email = False
}
SrtLst.Add(Srtitem)
'--------------------------------------------------------------------------------------------
Dim PrtLst As New List(Of JsonSettings.PRTTR)
Dim Prtitem As New JsonSettings.PRTTR With {
.Type = "PRT",
.Lumpsum = True,
.Email = False
}
PrtLst.Add(Prtitem)
Dim Mst1 As New List(Of JsonSettings.Master)
Dim mstitem As New JsonSettings.Master
mstitem.acList = AcmsList
mstitem.prList = PrmsList
Mst1.Add(mstitem)
'--------------------------------------------------------------------------------------------
Dim Trn1 As New List(Of JsonSettings.Transact)
Dim trnitem As New JsonSettings.Transact
trnitem.SalList = SalLst
trnitem.PurList = PurLst
trnitem.SrtList = SrtLst
trnitem.PrtList = PrtLst
Trn1.Add(trnitem)
'--------------------------------------------------------------------------------------------
Dim YearList As New List(Of JsonSettings.AccYear)
Dim YearItem As New JsonSettings.AccYear With {
.YearNo = "19201",
.MST = Mst1,
.TRN = Trn1
}
YearList.Add(YearItem)
'--------------------------------------------------------------------------------------------
st.Yr = YearList
Dim output As String = JsonConvert.SerializeObject(st, Formatting.Indented)
Clipboard.SetText(output)
Catch ex As Exception
MessageBox.Show(ex.message)
End Try
End Sub
我实现了以下解决方案:
{
"Yr": [ '--- THIS ARRAY IS GOOD AND REQUIRED.
{
"YearNo": "19201",
"MST": [ '---- HOW DO I WRITE THIS WITHOUT ARRAY??
{
"prList": {
"Type": "PR",
"SaleRate": true
},
"acList": {
"Type": "AC",
"PAN": true
}
}
],
"TRN": [ '---- HOW DO I WRITE THIS WITHOUT ARRAY??
{
"SalList": [
{
"Type": "SAL",
"Lumpsum": false,
"Email": false
}
],
"PurList": [ '---- HOW DO I WRITE THIS WITHOUT ARRAY??
{
"Type": "PUR",
"Lumpsum": false,
"Email": false
}
],
"SrtList": [ '---- HOW DO I WRITE THIS WITHOUT ARRAY??
{
"Type": "SRT",
"Lumpsum": false,
"Email": false
}
],
"PrtList": [ '---- HOW DO I WRITE THIS WITHOUT ARRAY??
{
"Type": "PRT",
"Lumpsum": true,
"Email": false
}
]
}
]
}
]
}
我尝试了很多东西,但我可能会遗漏一些东西来获得以下解决方案。上述解决方案对我有好处。但我想要下面这样的东西:
{
"Yr": [
{
"YearNo": "19201",
"MST": {
"prList": {
"Type": "PR",
"SaleRate": true
},
"acList": {
"Type": "AC",
"PAN": true
}
},
"TRN": {
"SalList": {
"Type": "SAL",
"Lumpsum": false,
"Email": false
},
"PurList": {
"Type": "PUR",
"Lumpsum": false,
"Email": false
},
"SrtList": {
"Type": "SRT",
"Lumpsum": false,
"Email": false
},
"PrtList": {
"Type": "PRT",
"Lumpsum": true,
"Email": false
}
}
},
{
"YearNo": "19201",
"MST": {
"prList": {
"Type": "PR",
"SaleRate": true
}
},
"TRN": {
"SalList": {
"Type": "SAL",
"Lumpsum": false,
"Email": false
},
"PurList": {
"Type": "PUR",
"Lumpsum": false,
"Email": false
},
"SrtList": {
"Type": "SRT",
"Lumpsum": false,
"Email": false
},
"PrtList": {
"Type": "PRT",
"Lumpsum": true,
"Email": false
}
}
}
]
}
解决方案
看起来你只需要让它们不是List
s,因为:
Public Class AccYear
Public Property YearNo As String
Public Property MST As Master 'instead of List(Of Master)
Public Property TRN As Transact 'instead of List(Of Transact)
End Class
...
Public Class Transact
Public Property SalList As List(Of SALTR)
Public Property PurList As PURTR 'instead of List(Of PURTR)
Public Property SrtList As SRTTR 'instead of List(Of SRTTR)
Public Property PrtList As PRTTR 'instead of List(Of PRTTR)
End Class
(顺便说一句,后三个项目被调用*List
,所以你需要确保它们不是数组)
推荐阅读
- c# - 如何从 ZKTeco 设备获取缺勤天数?
- css - 内联网格与网格?
- java - 无法转换为 org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreaker 类
- layer - QGIS保存层
- excel - VBA(Excel宏)通过输入框捕获变量/值并将该值作为公式的一部分传递给单元格
- sql - 将sql语句转换为pyspark
- makefile - Makefile 无法识别模式规则
- python - 在熊猫中创建增量日期列时输入错误
- mysql - 查询加入另一个 MySQL 表,计算行数和特定字段
- python - 列表对数组有什么作用?