Home > Software engineering >  How to format JSON data with brackets []
How to format JSON data with brackets []

Time:09-11

I have write the follow code to get the data formated as JSON

{"billNumber":"0003","entityActivityId":572,"customerFullName":"التوكيلات التجارية","customerMobileNumber":"0506436506","customerPreviousBalance":0,"issueDate":"2022-08-16","expireDate":"2022-09-23","billItemList":{"name":"*","quantity":1,"unitPrice":900,"discount":0,"discountType":"FIXED","vat":"0.15"}}

but I want to be as follwoing:

the deferrence is the [] in the details data.

{"billNumber":"0003","entityActivityId":572,"customerFullName":"التوكيلات التجارية","customerMobileNumber":"0506436506","customerPreviousBalance":0,"issueDate":"2022-08-16","expireDate":"2022-09-23","billItemList":[{"name":"*","quantity":1,"unitPrice":900,"discount":0,"discountType":"FIXED","vat":"0.15"}]}

my code is :

dt1 = dt.ToString("yyyy-MM-dd")
        dt2 = dt.AddDays(7).ToString("yyyy-MM-dd")
        Dim b As New billItemList With
        {
        .name = "*",
        .quantity = 1,
        .unitPrice = txtSubTotal.Value,
        .discount = txtDiscountValue.Value,
        .discountType = "FIXED",
        .vat = 0.15
        }

        
        Dim person = New EfaaAPI() With {
        .billNumber = txtInvNo.Value,
        .entityActivityId = 572,
        .customerFullName = CN,
        .customerMobileNumber = txtTelNo.Value,
        .issueDate = dt1,
        .expireDate = dt2,
                     .billItemList = b
         }

        Dim json = JsonConvert.SerializeObject(person)

my class is :

Public Class EfaaAPI


Public Property billNumber As String 'optional
Public Property entityActivityId As Integer
Public Property customerFullName As String
Public Property customerMobileNumber As String
Public Property customerPreviousBalance As Integer
Public Property issueDate As String  'YYYY-MM-DD
Public Property expireDate As String 'YYYY-MM-DD
Public Property billItemList As New billItemList()

End Class

Public Class billItemList
Public Property name As String
Public Property quantity As Integer
Public Property unitPrice As Integer
Public Property discount As Integer
Public Property discountType As String
Public Property vat As String

End Class

any idea regarding this issue ??

CodePudding user response:

try changing :

Public Class EfaaAPI
Public Property billNumber As String 'optional
Public Property entityActivityId As Integer
Public Property customerFullName As String
Public Property customerMobileNumber As String
Public Property customerPreviousBalance As Integer
Public Property issueDate As String  'YYYY-MM-DD
Public Property expireDate As String 'YYYY-MM-DD
Public Property billItemList As New billItemList()
End Class

to

Public Class EfaaAPI
Public Property billNumber As String 'optional
Public Property entityActivityId As Integer
Public Property customerFullName As String
Public Property customerMobileNumber As String
Public Property customerPreviousBalance As Integer
Public Property issueDate As String  'YYYY-MM-DD
Public Property expireDate As String 'YYYY-MM-DD
Public Property billItemList As New List(Of billItemList)()
End Class

and try changing :

Dim person = New EfaaAPI() With {
         .billNumber = txtInvNo.Value,
         .entityActivityId = 572,
         .customerFullName = CN,
         .customerMobileNumber = txtTelNo.Value,
         .issueDate = dt1,
         .expireDate = dt2,
         .billItemList = b 
     }

to :

Dim person = New EfaaAPI() With {
        .billNumber = "d",
        .entityActivityId = 572,
        .customerFullName = "ss",
        .customerMobileNumber = "ss",
        .issueDate = "ss",
        .expireDate = "ds",
        .billItemList = New List(Of billItemList)({b})

       }

CodePudding user response:

you need to fix the class

     Public Class EfaaAPI
        // .... another properties
        Public Property billItemList As List(Of billItem)
     End Class

change the name of class

Public Class billItem
Public Property name As String
//.....
End Class

and code

  Dim b As New List(Of billItem) From
        {
    New billItem With {
      .name = "d",
      .quantity = 572,
     //...
    }
}
 Dim person = New EfaaAPI With {
        //....
        .expireDate = dt2,
        .billItemList = b
         }
  • Related