Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Try to creating a JSON file using vb.net and json.net in windows application but unable to generate desired JSON file format.

JSON format:
{
  "CompCode": "COMP0001",
  "fp": "072018",
  "gt": 0,
  "cur_gt": 0,
  "b2b": [
    {
      "patry": "ABC  Company",
      "inv": [
        {
          "inum": "00164",
          "idt": "01-07-2018",
          "val": 525,
          "itms": [
            {
              "num": 1,
              "itm_det": {
                "txval": 500,
                "rt": 5
              }
            }
          ]
        }
      ]
    }
  ]
}


What I have tried:

<pre lang="vb">

  Private Sub cmdGenerateJson_Click(sender As Object, e As EventArgs) Handles cmdGenerateJson.Click
    Dim JsonHeader As New ClsHeader()
    ''Main 
    JsonHeader.compcode = "COMP0001"
    JsonHeader.fp = "0.00"
    JsonHeader.gt = 0
    JsonHeader.cur_gt = 0
    ''Party  
    Dim b2 As New ClsParty
    b2.party = "ABC Company"
    ''Inv  
    Dim InvDet As New ClsInvoice
    InvDet.inum = 111
    InvDet.idt = "25-08-2018"
    InvDet.val = 525
    ''Item  
    Dim cid As New ClsItem
    cid.num = 1
    ''Item details 
    Dim itmdet As New ClsItemDetails
    itmdet.txval = 500
    itmdet.rt = 5
    JsonHeader.b2b.Add(b2)
    Dim jsonString As String = JsonConvert.SerializeObject(JsonHeader)
    TextBox1.Text = jsonString
  End Sub
End Class

Public Class ClsHeader
  Public Property compcode As String
  Public Property fp As String
  Public Property gt As Int64
  Public Property cur_gt As Int64
  Public Property b2b As New List(Of ClsParty)
End Class

Public Class ClsParty
  Public Property party As String
  Public Property inv As New List(Of ClsInvoice)
End Class

Public Class ClsInvoice
  Public Property inum As String
  Public Property idt As String
  Public Property val As Double
  Public Property itms As New List(Of ClsItem)
End Class

Public Class ClsItem
  Public Property num As Integer
  Public Property itm_det As New List(Of ClsItemDetails)
End Class

Public Class ClsItemDetails
  Public Property txval As Double
  Public Property rt As Double
End Class
Posted
Updated 22-Apr-20 18:01pm
Comments
Patrice T 1-Sep-18 9:58am    
What output do you get ?

1 solution

I wrote an article to help answer questions commonly asked in here: Working with JSON in C# & VB[^]

Your question is about serialization of class to raw JSON, something not covered in detail in the article, so I will answer here using Newtonsoft's Json.NET[^] library.

To make sure that your classes match the JSON structure, I've used a utility website jsonutils[^] (currently offline at the time of writing this answer - you can download and run the website locally: GitHub - bladefist/JsonUtils[^]). IT will generate the following set of classes:
VB
Public Class ItmDet
    <JsonProperty("txval")>
    Public Property Txval As Integer

    <JsonProperty("rt")>
    Public Property Rt As Integer
End Class

Public Class Itm
    <JsonProperty("num")>
    Public Property Num As Integer

    <JsonProperty("itm_det")>
    Public Property ItmDet As ItmDet
End Class

Public Class Inv
    <JsonProperty("inum")>
    Public Property Inum As String

    <JsonProperty("idt")>
    Public Property Idt As String

    <JsonProperty("val")>
    Public Property Val As Integer

    <JsonProperty("itms")>
    Public Property Itms As Itm()
End Class

Public Class B2b
    <JsonProperty("patry")>
    Public Property Patry As String

    <JsonProperty("inv")>
    Public Property Inv As Inv()
End Class

Public Class Example
    <JsonProperty("CompCode")>
    Public Property CompCode As String

    <JsonProperty("fp")>
    Public Property Fp As String

    <JsonProperty("gt")>
    Public Property Gt As Integer

    <JsonProperty("cur_gt")>
    Public Property CurGt As Integer

    <JsonProperty("b2b")>
    Public Property B2b As B2b()
End Class

Then in the article link above I have a helper class for serializing and deserializing To/From JSON. Here is the class from the article:
VB
Public Module JsonHelper

    Public Function FromClass(Of T)(data As T,
                                    Optional isEmptyToNull As Boolean = False,
                                    Optional jsonSettings As JsonSerializerSettings = Nothing) As String

        Dim response As String = String.Empty

        If Not EqualityComparer(Of T).Default.Equals(data, Nothing) Then
            response = JsonConvert.SerializeObject(data, jsonSettings)
        End If

        Return If(isEmptyToNull, (If(response = "{}", "null", response)), response)

    End Function

    Public Function ToClass(Of T)(data As String,
                                  Optional jsonSettings As JsonSerializerSettings = Nothing) As T

        Dim response = Nothing

        If Not String.IsNullOrEmpty(data) Then
            response = If(jsonSettings Is Nothing,
                          JsonConvert.DeserializeObject(Of T)(data),
                          JsonConvert.DeserializeObject(Of T)(data, jsonSettings))
        End If

        Return response

    End Function

End Module

Now to use, it is simply as follows:
VB
Sub Main()

    Dim rawJson = "{""CompCode"":""COMP0001"",""fp"":""072018"",""gt"":0,""cur_gt"":0,""b2b"":[{""patry"":""ABC  Company"",""inv"":[{""inum"":""00164"",""idt"":""01-07-2018"",""val"":525,""itms"":[{""num"":1,""itm_det"":{""txval"":500,""rt"":5}}]}]}]}"

    ' From JSON to Class
    Dim resultClass = JsonHelper.ToClass(Of Example)(rawJson)

    ' From Class to raw JSON
    Dim resultJson = JsonHelper.FromClass(resultClass)

End Sub
 
Share this answer
 
Comments
S4753 2-Sep-18 1:44am    
Article very helpful but need your help create RawJson in systematic way and fast.
Graeme_Grant 2-Sep-18 4:16am    
See the answer above. I've just shown you how to deserialize and serialize. The same principle is used if you create the class data yourself before serializing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900