Solved

Creating a JSON Request using VB.NET

Posted on 2016-08-25
7
51 Views
Last Modified: 2016-10-03
I have to create a JSON request in the following format

{
  "companyId": 1111,
  "shipDate": "2016-08-25",
  "notes": "ABC",
  "Items":
   [
    {
      "productId": 111,
      "Type": "9",
      "Qty": 2,
      "cost": 3,
      "price": 3.50,    
    }
 ]
 }


So far, I have created two classes:

Public Class SaleOrder
        Public Property companyId As Integer
        Public Property shipDate As Date
        Public Property notes As String        
        Public Property Items As Items
    End Class

    Public Class Items
        Public Property productId As Integer
        Public Property Type As String
        Public Property Qty As Integer
        Public Property cost As Double
        Public Property price As Double
    End Class

Below is my code in VB.NET


Dim OrderList As New List(Of SaleOrder()
Dim SaleOrder As New SaleOrder()

SaleOrder.companyId= "1111"
SaleOrder.notes = "ABC"
SaleOrder.shipDate = Now.ToString("yyyy-MM-dd")

SaleOrder.Items.productId = 111
SaleOrder.Items.Type = "9"
SaleOrder.Items.Qty = 3
SaleOrder.Items.cost = 2
SaleOrder.Items.price = 3.5

OrderList.Add(SaleOrder)


I am getting "object reference not set to instance..." error on
SaleOrder.Items.productId

It the way I am trying to create the Items array wrong?
0
Comment
Question by:Angel02
  • 4
  • 2
7 Comments
 
LVL 4

Assisted Solution

by:rotem156
rotem156 earned 150 total points
ID: 41770855
You're welcome.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41770864
change;
Public Property Items As Items

Open in new window


for
Public Property Items As new list(of Items)

Open in new window

0
 

Author Comment

by:Angel02
ID: 41770868
Thanks! Got it. I made the following change

Dim Item As New Items()

Item.productId = 111
Item.Type = "9"
Item.Qty = 3
Item.cost = 2
Item.price = 3.5

SaleOrder.Item = Item
OrderList.Add(SaleOrder)

This created the JSON request as

[{
  "companyId": 1111,
  "shipDate": "2016-08-25",
  "notes": "ABC",
  "Items":
    {
      "productId": 111,
      "Type": "9",
      "Qty": 2,
      "cost": 3,
      "price": 3.50,    
    }
 }]

Items is created as an object. How to I convert it to array so it looks exactly like the sample in my question?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41770889
have you tried :
Public Property Items As new list(of Items)

Open in new window

0
 

Author Comment

by:Angel02
ID: 41771800
If I declare Items as

Public Property Items As new list(of Items)

Can you please let me know how do I assign it values?

Item.productId = 111
Item.Type = "9"
Item.Qty = 3
Item.cost = 2
Item.price = 3.5

as the above wouldn't work anymore.
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 350 total points
ID: 41771844
if you would like to be clean, your class:
Public Class Items

Open in new window


should be declared as:
Public Class Item

Open in new window


because a class instance contains only 1 item

you will have to change your property for:
Public Property Items As new list(of Item)

Open in new window


then you do something like to create an instance of the class:
dim x as new Item
x.productId = 111
x.Type = "9"
x.Qty = 3
x.cost = 2
x.price = 3.5

Open in new window


to finally add your item to the collection:
SaleOrder.Items.Add(x)

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41771845
it is not in the best practices to name a class instance the same as the declaration:
Dim SaleOrder As New SaleOrder()

Open in new window


One of the 2 should not be named SaleOrder!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now