在Windows应用程序中使用VB.NET和json.net创建JSON [英] Create JSON using VB.NET and json.net in windows application

查看:96
本文介绍了在Windows应用程序中使用VB.NET和json.net创建JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Windows应用程序中使用vb.net和json.net创建JSON文件,但无法生成所需的JSON文件格式。



JSON格式:

 {
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
}
}
]
}
]
}
]
}





我的尝试:



< pre lang =vb> 

Private Sub cmdGenerateJson_Click(sender As Object,e As EventArgs)处理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公司
''Inv $​​ b $ b Dim InvDet As New ClsInvoice
InvDet.inum = 111
InvDet.idt =25-08-2018
InvDet.val = 525
''项目
Dim cid As New ClsItem
cid.num = 1
''项目详情
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
Pub lic属性fp As String
公共属性gt As Int64
公共属性cur_gt为Int64
公共属性b2b为新列表(Of ClsParty)
结束类

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类ClsItem
公共属性num作为整数
公共属性itm_det作为新列表(Of ClsItemDetails)
结束类

公共类ClsItemDetails
公共属性txval As Double
Public Property rt As Double
End Class

解决方案

我写了一篇文章来帮助回答常见问题在这里:在C#中使用JSON& VB [ ^ ]



您的问题是关于类到原始JSON的序列化,文章中没有详细介绍,所以我将在这里使用Newtonsoft的 Json.NET [ ^ ]库。



为了确保您的类与JSON结构匹配,我使用了一个实用程序网站jsonutils [ ^ ](撰写本文时目前处于离线状态这个答案 - 你可以在本地下载并运行网站: GitHub - bladefist / JsonUtils [ ^ ])。 IT将生成以下一组课程:

 公共  ItmDet 
< JsonProperty( txval)>
公开 属性 Txval 作为 整数

< JsonProperty( RT)GT;
公开 属性 Rt 作为 整数
结束

公共 Itm
< JsonProperty( num)>
公开 属性 Num 作为 整数

< JsonProperty( itm_det)GT;
公开 属性 ItmDet 作为 ItmDet
结束

公共 Inv $​​ b $ b< JsonProperty( INUM)GT;
公开 属性 Inum 作为 字符串

< JsonProperty( IDT)GT;
公开 属性 Idt 作为 字符串

< JsonProperty( VAL)GT;
公开 属性 Val 作为 整数

< JsonProperty( ITMS)GT;
公开 属性 Itms 作为 Itm()
结束

公共 B2b
< JsonProperty( < span class =code-string> patry
)>
公开 属性 Patry 作为 字符串

< JsonProperty( INV)GT;
公开 属性 Inv 作为 Inv()
结束

公共 示例
< JsonProperty( < span class =code-string> CompCode
)>
公开 属性 CompCode 作为 字符串

< JsonProperty( FP)GT;
公开 属性 Fp 作为 字符串

< JsonProperty( GT)GT;
公开 属性 Gt 作为 整数

< JsonProperty( cur_gt)GT;
公开 属性 CurGt 作为 整数

< JsonProperty( B2B)GT;
公开 属性 B2b 作为 B2b()
结束



然后在上面的文章链接中,我有一个帮助类,用于序列化和反序列化To / From JSON。以下是文章中的类:

 公共 模块 JsonHelper 

公共 功能 FromClass( Of T)(data As T,
可选 isEmptyToNull As Boolean = False
可选 jsonSettings 作为 JsonSerializerSettings = Nothing 作为 字符串

Dim 响应作为 字符串 = 字符串 .Empty

如果 EqualityComparer( Of T)。默认 .Equals(data, Nothing 然后
response = JsonConvert.SerializeObject(data,jsonSettings)
结束 如果

返回 If (isEmptyToNull,( If (response = {} null,response)) ,响应)

结束 功能

< span class =code-keyword>公共 功能 ToClass( of T)(data As String
可选 jsonSettings 作为 JsonSerializerSettings = Nothing 作为 T

Dim response = Nothing

如果 字符串 .IsNullOrEmpty(数据)然后
response = 如果(jsonSettings Nothing
JsonConvert .DeserializeObject( Of T)(数据),
JsonConver t.DeserializeObject( Of T)(data,jsonSettings))
结束 如果

返回响应

结束 功能

结束 模块



现在使用,它简单如下:

  Sub  Main()

Dim rawJson = {CompCode:COMP0001,fp:072018,gt :0,cur_gt:0,b2b:[{patry:ABC Company,inv:[{inum:00164 IDT : 2018年1月7日 , VAL :525 ITMS :[{ NUM :1 itm_det :{ txval:500,rt:5}}]}]}]}

' 来自JSO N to Class
Dim resultClass = JsonHelper.ToClass( of 示例)( rawJson)

' 从Class到raw JSON
Dim resultJson = JsonHelper.FromClass(resultClass)

End


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

解决方案

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:

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:

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:

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


这篇关于在Windows应用程序中使用VB.NET和json.net创建JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆