如何使用Properties和SubProperties编写JSON

时间:2017-01-27 12:46:47

标签: json vb.net json.net

我需要编写并向Web服务发送JSON字符串。 我必须创建一个JSON,如下所示

i < 10

如何使用newntonsoft类进行此操作?

2 个答案:

答案 0 :(得分:0)

您需要创建相应的类来创建这样的JSON,然后您可以序列化它们的对象以创建此JSON。

我不是VB.NET的人,所以我用C#编写了这个,并使用一些在线工具转换为VB.NET代码。

Imports Newtonsoft.Json
Imports System.Collections.Generic

Public Class SalesContainer

   <JsonProperty(PropertyName:="SALES")>  _
   Public Property Sales As List(Of SalesClass)

End Class

Public Class SalesClass

    <JsonProperty(PropertyName:="INDEX_NX")>  _
    Public Property Index As Integer

    <JsonProperty(PropertyName:="Audios")>  _
    Public Property Audio As List(Of AudioClass)

    <JsonProperty(PropertyName:="Documents")>  _
    Public Property Documents As List(Of DocumntClass)

End Class

Public Class AudioClass

    Public Property Name As String

    Public Property Content As String

    Public Property Extension As String

    Public Property Id As Integer

End Class

Public Class DocumentClass

    Public Property Name As String

    Public Property Content As String

    Public Property Extension As String

    Public Property Document_type As String

    Public Property Type_Document As String

End Class

您可以按照以下方式创建JSON。

Imports System
Imports Newtonsoft.Json
Imports System.Collections.Generic

Public Module mainModule  
    Sub Main()  
        Dim salescontainer AS New SalesContainer
        salescontainer.Sales = New List(Of SalesClass)

        Dim sales As New SalesClass

        sales.Audio = New List(Of AudioClass)
        sales.Audio.Add(New AudioClass() With {.Id=1236585, .Name="audio_file", .Content="string_base64", .Extension="WAV"})

        sales.Documents = New List(Of DocumntClass)()
        sales.Documents.Add(New DocumntClass() With {.Name="proposal_based", .Content="string_base64", .Extension="PNG", .Type_Document="PROPOSAL SIGNED"})
        sales.Documents.Add(New DocumntClass() With {.Name="document_with_photo", .Content="string_base64", .Extension="PNG", .Document_type="CNH"})

        salescontainer.Sales.Add(sales)

        Dim serializerSettings As New JsonSerializerSettings
        serializerSettings.NullValueHandling = NullValueHandling.Ignore

        Dim jsonData = JsonConvert.SerializeObject(salescontainer, serializerSettings)
        Console.WriteLine(jsonData)

    End Sub  
End Module

答案 1 :(得分:0)

在Visual Studio 2015(以及可能的早期版本)中,您可以启动一个新的类代码文件,并在剪贴板中使用该开放且格式正确的JSON,Edit菜单中有Paste Special&gt; Paste JSON as Classes,做得不错(但不太完美)。从那里,我们得到下面的类,以及一个从这些类生成JSON的小模块。

Imports Newtonsoft.Json

Module Module1

    Sub Main()
        Dim Docs As New List(Of Document)
        Docs.Add(New Document With {
            .Name = "proposal_based",
            .Content = "string_base64",
            .Extension = "PNG",
            .Type_document = "PROPOSAL"})
        Docs.Add(New Document With {
            .Name = "document_with_photo",
            .Content = "string_base64",
            .Extension = "PNG",
            .Document_type = "CNH"})

        Dim Rootobject As New Rootobject
        With Rootobject
            .SALES = {New SALE With {
                .INDEX_NX = 1,
                .Audios = {New Audio With {
                    .Name = "audio_file",
                    .Content = "string_base64",
                    .Extension = "WAV",
                    .Id = "1236585"}},
                .Documents = Docs.ToArray}}
        End With

        Dim Settings As New JsonSerializerSettings
        Settings.Formatting = Formatting.Indented
        Settings.NullValueHandling = NullValueHandling.Ignore

        Dim JSON As String = JsonConvert.SerializeObject(Rootobject, Settings)

        Debug.Print(JSON)
        ' Submit JSON to your web service
    End Sub

End Module

Public Class Rootobject
    Public Property SALES As SALE()
End Class

Public Class SALE
    Public Property INDEX_NX As Integer
    Public Property Audios As Audio()
    Public Property Documents As Document()
End Class

Public Class Audio
    Public Property Name As String
    Public Property Content As String
    Public Property Extension As String
    Public Property Id As String
End Class

Public Class Document
    Public Property Name As String
    Public Property Content As String
    Public Property Extension As String
    Public Property Type_document As String
    Public Property Document_type As String
End Class