我正在学习通过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",.Email = False
}
PurLst.Add(Puritem)
'--------------------------------------------------------------------------------------------
Dim SrtLst As New List(Of JsonSettings.SRTTR)
Dim Srtitem As New JsonSettings.SRTTR With {
.Type = "SRT",.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","SrtList": [ '---- HOW DO I WRITE THIS WITHOUT ARRAY??
{
"Type": "SRT","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","Email": false
},"PurList": {
"Type": "PUR","SrtList": {
"Type": "SRT","PrtList": {
"Type": "PRT","Email": false
}
}
},{
"YearNo": "19201","SaleRate": true
}
},"Email": false
}
}
}
]
}