Как создать объект JSON в VB.NET - PullRequest
1 голос
/ 07 ноября 2019

Я учусь создавать json через классы vb.net. Я знаю, как создать массив 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
        }
      }
    }
  ]
}

1 Ответ

0 голосов
/ 07 ноября 2019

Похоже, вам просто нужно сделать так, чтобы они не были List с, как:

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

(кстати, последние 3 элемента называются *List, поэтому вам нужно обязательно сделать ихне быть массивами)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...