【问题标题】:Serialization of nested JSON with objects使用对象序列化嵌套 JSON
【发布时间】:2019-07-11 22:09:11
【问题描述】:

我需要创建一个如下所示的 JSON。

{
    "ShipmentId": "111888",
    "BizId": "MORRIS",
    "BizSalesOrder": null,
    "Status": "00",
    "OrderType": "S01",
    "OrderClass": "PACKSHIP",
    "ShipmentLines": [
      {
        "ShipmentId": "111888",
        "QtyOrdered": 30.00,
        "QtyRequired": 30.00,
        "QtyDueOut": 0.00,
        "SOLineId": 1.00,
        "Stage": "90"
      },
      {
        "ShipmentId": "111888",
        "QtyOrdered": 40.00,
        "QtyRequired": 40.00,
        "QtyDueOut": 0.00,
        "SOLineId": 2.00,
        "Stage": "90"
      },
      {
        "ShipmentId": "111888",
        "QtyOrdered": 10.00,
        "QtyRequired": 10.00,
        "QtyDueOut": 0.00,
        "SOLineId": 3.00,
        "Stage": "90"
      },
      {
        "ShipmentId": "111888",
        "QtyOrdered": 5.00,
        "QtyRequired": 5.00,
        "QtyDueOut": 0.00,
        "SOLineId": 4.00,
        "Stage": "90"
      }
    ],
    "ShipAddress": [
      {
        "Table": "SHH",
        "ShipmentId": "111888",
        "AddressId": "ADD1",
        "Name": "John Smith",
        "Line1": "20 Michelin Ave",
        "Line2": null,
        "City": "LONDON",
        "Postcode": "A99 9BC",
        "Country": "GB"
      }
    ],
    "ShipContacts": [],
    "ShipmentDespatch": []
  }

我有来自http://www.jsonutils.com/(下)的课程,但我很确定 ShipmentLines 应该是“列表(ShipmentLine)”,但这对我来说仍然很困惑..

Public Class ShipmentLine
    Public Property ShipmentId As String
    Public Property QtyOrdered As Double
    Public Property QtyRequired As Double
    Public Property QtyDueOut As Double
    Public Property SOLineId As Double
    Public Property Stage As String
End Class

Public Class ShipAddress
    Public Property Table As String
    Public Property ShipmentId As String
    Public Property AddressId As String
    Public Property Name As String
    Public Property Line1 As String
    Public Property Line2 As Object
    Public Property City As String
    Public Property Postcode As String
    Public Property Country As String
End Class

Public Class Shipment
    Public Property ShipmentId As String
    Public Property BizId As String
    Public Property BizSalesOrder As Object
    Public Property Status As String
    Public Property OrderType As String
    Public Property OrderClass As String
    Public Property ShipmentLines As List(Of ShipmentLine)
    Public Property ShipAddress As List(Of ShipAddress)
    Public Property ShipContacts As Object()
    Public Property ShipmentDespatch As Object()
End Class

我可以执行 1 级 JSON(如下),但我在 1 个嵌套问题中存在嵌套(和)多个对象的问题。

我是 VB.NET 的新手,并且是编程爱好者(如您所见),因此我们将不胜感激。

Shipping Lines 都在 Listview1 中(从左到右从 ShipmentId 到 Stage),所以我不确定如何获取它们并将它们从 Listview1 序列化为 JSON。

如果你能帮我写一个关于如何序列化我下面的代码,但这只是json的一级,没有嵌套......

请帮忙,我经常浏览 StackOverflow 论坛,现在我只是重复所有帖子,我无法理解它们......

Dim Ship As New Shipment

Ship.ShipmentId = TextBox1.Text
Ship.BizId = TextBox2.Text
Ship.Status = "00"
Ship.OrderType = TextBox3.Text
Ship.Type = TextBox4.Text
Ship.OrderClass = TextBox5.Text


Dim json As String = JsonConvert.SerializeObject(Ship).ToString

【问题讨论】:

  • 您的装运线来自哪里?另一个文本框,或一些字符串,或您需要解析的输入数组或什么?你能发一下吗?如此接近发布一些东西,但如果我们知道这些行来自哪里以及它们是什么格式,那真的会有所帮助:)
  • 嗨 Stuart,行位于 ListView1 框中,其中包含一组列。主组是 SOLineId,其余的是子组
  • 贴出来让我们看看它是什么样子的,你可能需要拆分字符串和所有排序
  • 我不知道怎么做。第一列是 SOLineId,第二列是订购的数量,然后是第三列所需的数量等,每列中都有值,就像它们在 ListView 中一样。抱歉,这可能帮不上什么忙。我可以张贴一张照片吗?
  • 图片可能会有所帮助,很难理解原始数据仅此而已,一旦清楚,将其解析到您的列表中应该很简单......

标签: json vb.net serialization nested


【解决方案1】:

这可以通过多种方式完成,但我遵循你所做的。

这就是我所做的:

Imports Newtonsoft.Json

Public Class Form1

    Public Class Shipment
        Public Property ShipmentId As String
        Public Property BizId As String
        Public Property BizSalesOrder As Object
        Public Property Status As String
        Public Property OrderType As String
        Public Property OrderClass As String
        Public Property ShipmentLines As List(Of ShipmentLine)
        Public Property ShipAddress As ShipAddress
        Public Property ShipContacts As Object()
        Public Property ShipmentDespatch As Object()
    End Class

    Public Class ShipmentLine
        Public Property ShipmentId As String
        Public Property QtyOrdered As Double
        Public Property QtyRequired As Double
        Public Property QtyDueOut As Double
        Public Property SOLineId As Double
        Public Property Stage As String
    End Class

    Public Class ShipAddress
        Public Property Table As String
        Public Property ShipmentId As String
        Public Property AddressId As String
        Public Property Name As String
        Public Property Line1 As String
        Public Property Line2 As Object
        Public Property City As String
        Public Property Postcode As String
        Public Property Country As String
    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim NewShipment As New Shipment With {
            .ShipmentId = "111888",
            .BizId = "MORRIS",
            .BizSalesOrder = "null",
            .Status = "00",
            .OrderType = "S01",
            .OrderClass = "PACKSHIP"
        }
        Dim NewShipmentLines As New List(Of ShipmentLine)
        For x = 0 To 3
            Dim NewShipmentLine As New ShipmentLine
            NewShipmentLine.ShipmentId = "111888"
            NewShipmentLine.QtyOrdered = x
            NewShipmentLine.QtyDueOut = x
            NewShipmentLine.SOLineId = x
            NewShipmentLine.Stage = x
            NewShipmentLines.Add(NewShipmentLine)
        Next
        NewShipment.ShipmentLines = NewShipmentLines
        Dim NewShipAdress As New ShipAddress With {
            .Table = "SHH",
            .ShipmentId = "111888",
            .AddressId = "ADD1",
            .Name = "John Smith",
            .Line1 = "20 Michelin Ave",
            .Line2 = "null",
            .City = "LONDON",
            .Postcode = "A99 9BC",
            .Country = "GB"
        }
        NewShipment.ShipAddress = NewShipAdress
        NewShipment.ShipContacts = Nothing
        NewShipment.ShipmentDespatch = Nothing

        Dim ResultJSON As String = JsonConvert.SerializeObject(NewShipment).ToString
        Debug.Print(ResultJSON) '
    End Sub
End Class

结果:

{
   "ShipmentId":"111888",
   "BizId":"MORRIS",
   "BizSalesOrder":"null",
   "Status":"00",
   "OrderType":"S01",
   "OrderClass":"PACKSHIP",
   "ShipmentLines":[
      {
         "ShipmentId":"111888",
         "QtyOrdered":0.0,
         "QtyRequired":0.0,
         "QtyDueOut":0.0,
         "SOLineId":0.0,
         "Stage":"0"
      },
      {
         "ShipmentId":"111888",
         "QtyOrdered":1.0,
         "QtyRequired":0.0,
         "QtyDueOut":1.0,
         "SOLineId":1.0,
         "Stage":"1"
      },
      {
         "ShipmentId":"111888",
         "QtyOrdered":2.0,
         "QtyRequired":0.0,
         "QtyDueOut":2.0,
         "SOLineId":2.0,
         "Stage":"2"
      },
      {
         "ShipmentId":"111888",
         "QtyOrdered":3.0,
         "QtyRequired":0.0,
         "QtyDueOut":3.0,
         "SOLineId":3.0,
         "Stage":"3"
      }
   ],
   "ShipAddress":{
      "Table":"SHH",
      "ShipmentId":"111888",
      "AddressId":"ADD1",
      "Name":"John Smith",
      "Line1":"20 Michelin Ave",
      "Line2":"null",
      "City":"LONDON",
      "Postcode":"A99 9BC",
      "Country":"GB"
   },
   "ShipContacts":null,
   "ShipmentDespatch":null
}

显然,您需要对其进行调整以适合您,但它确实有效。对不起,我没有发表任何评论,如果您有任何问题,请提出。

【讨论】:

  • 谢谢@CruleD,它很有帮助,我还需要修改一些地方,但是主机是可以理解的,谢谢!
  • 我确定这是一个愚蠢的问题.. 但我如何将其标记为已回答??
  • 感谢 CruleD,现在已经完成了,抱歉我是个新手!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
相关资源
最近更新 更多