1

I need to create a JSON that will look as below.

{
    "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": []
  }

I have classes from http://www.jsonutils.com/ (below) yet I'm pretty sure that ShipmentLines should be a "list(of ShipmentLine)", but that's still quite confusing for me..

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

I can do 1 level JSON (below), but i have problems with nested (and) multiple objects in 1 nest issue.

I'm new to VB.NET and an amateur in programming(as you can see), so clear instructions would be appreciated.

Shipping Lines are all in Listview1 (left to right from ShipmentId to Stage) so I'm not really sure how to get them and serialize them from Listview1 into a JSON neither.

If you could please write a code for me on how to serialize it what i have got below, but this is only level one of json, no nesting...

Please help, I've been through StackOverflow forum a lot and I'm just repeating all posts now and I can't understand them...

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
10
  • Where are your shipment lines coming from? a further textBox, or some string, or array of inputs you need to parse or what? Can you post that bit? So close to posting something but would really help if we know where those lines are coming form, and in what format they are :) Commented Jul 11, 2019 at 22:20
  • Hi Stuart, lines are in ListView1 box with a collection of columns. Main group is the SOLineId and the rest is subgroups Commented Jul 11, 2019 at 22:23
  • Post it so we can see what it looks like, you might need to be splitting strings and allsorts Commented Jul 11, 2019 at 22:24
  • I'm not sure how. In first column there's SOLineId, second there's Qty Ordered, then 3rd qty required etc in each column there are values as they would be in ListView. Sorry, that's probably not much help.. Can I post a picture somehow? Commented Jul 11, 2019 at 22:30
  • Picture may help, its hard to understand the originating data thats all, once thats clear it should be pretty straight forward to parse it into your lists... Commented Jul 11, 2019 at 22:31

1 Answer 1

1

It can be done in multiple ways but I followed what you did.

This is what I did:

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

And the result:

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

Obviously you will need to adapt it to work for you, but it works. Sorry I didn't comment anything, if you got questions, ask.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @CruleD, it's very helpful, i still need to change some bits on it, but main frame is understandable, thank you!
I'm sure this is a stupid question.. but how do I mark it as answered??
Thanks CruleD, that's done now, sorry for being such a newbie!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.