Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating a JSON Request using VB.NET

Posted on 2016-08-25
7
Medium Priority
?
99 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
[X]
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
  • 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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)…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

730 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