Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating a JSON Request using VB.NET

Posted on 2016-08-25
7
Medium Priority
?
109 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 600 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 1400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

886 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