我需要创建一个如下所示的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的新手,并且是编程的业余爱好者(如您所见),因此请您提供清晰的指令。
运输线都在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
答案 0 :(得分:0)
这可以通过多种方式完成,但是我遵循了您所做的事情。
这就是我所做的:
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
}
很显然,您需要对其进行调整以使其适合您,但是它可以工作。抱歉,我没有发表任何评论,如果您有任何疑问,请询问。