Solved

Creating a JSON Request using VB.NET

Posted on 2016-08-25
7
68 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 70

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 70

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 70

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 70

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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