Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Creating a JSON Request using VB.NET

Posted on 2016-08-25
7
74 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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