Best method - vb.net and reading a JSON response

18,232 views
Skip to first unread message

Toolma...@gmail.com

unread,
Aug 11, 2008, 4:58:10 PM8/11/08
to Jayrock
I am a vb.net developer who is writing an application that will call
an HTTP request that will return a JSON response.

The response that will be coming back looks something like this:

{
"logon": "jo...@fakesite.com",
"addresses": [
{
“shippingAddressCount”:”2”,
"shipping": [
{
"firstName": "John",
"lastName": "Smith",
"address1": "111 Fake St.",
"address2": "",
"city": "New York",
"state": "NY",
"zip": "11111"
},
{
"firstName": "John",
"lastName": "Smith",
"address1": "222 Fake St.",
"address2": "",
"city": "New York",
"state": "NY",
"zip": "11111"
}
],

"billing": {
"firstName": "John",
"lastName": "Smith",
"address1": "111 Fake St.",
"address2": "",
"city": "New York",
"state": "NY",
"zip": "11111"
},
"marketing": {
"addressId": "12345",
"firstName": "John",
"lastName": "Smith",
"address1": "111 Fake St.",
"address2": "",
"city": "New York",
"state": "NY",
"zip": "11111",
}
}
]
}

I am wondering what the best approach is for reading this response and
pulling the data elements out would be.

I have started with the JsonObject as a way to read the data but ran
into a road block with the "addresses" and how exactly to process
those.

Here is what I have so far (json.txt = data above):

----------------------------------------------------------------------------------------------------------
Dim oTextReader As System.IO.TextReader
oTextReader = New IO.StreamReader("json.txt")

Dim oJsonTextReader As New JsonTextReader(oTextReader)
Dim oJsonObject As New JsonObject

With oJsonObject
.Import(oJsonTextReader)
If .HasMembers Then
Response.Write("LogonID: " & .Item("logonId").ToString
& "<br>")
Response.Write("First Name: "
& .Item("firstName").ToString & "<br>")
End If
End With
----------------------------------------------------------------------------------------------------------

Am I headed down the right path? If so, how do I get and process the
addresses?

Thanks in advance for any help here!

Atif Aziz

unread,
Aug 12, 2008, 4:26:45 AM8/12/08
to jay...@googlegroups.com
> I have started with the JsonObject as a way to read the data but ran
> into a road block with the "addresses" and how exactly to process
> those.

When doing a generic import, the simple rule to remember is that JSON objects {...} become dictionaries (IDictionary) and JSON arrays [...] become lists (IList). The concrete types yielded are JsonObject and JsonArray if that's important to you.

Given your root oJsonObject, you can access the various pieces like this...

Get the logon member of the root object:

Dim logon As String = oJsonObject("logon")

Get the list of addresses from the root object:

Dim addresses As IList = oJsonObject("addresses")

Get the first address object:

Dim address As IDictionary = addresses(0)

Loop through each shipping address object and dump members:

For Each shipping In address("shipping")
Console.WriteLine(shipping("firstName"))
Console.WriteLine(shipping("lastName"))
' etc.
End

And so on...

Hope this clears things up.

- Atif

Toolma...@gmail.com

unread,
Aug 12, 2008, 9:39:19 AM8/12/08
to Jayrock
Thanks Atif. I'll make some notes below on the results I'm getting.
BTW, I'm in .net 2.0 and using JayRock version 0.9.8316

> When doing a generic import, the simple rule to remember is that JSON objects {...} become dictionaries (IDictionary) and JSON arrays [...] become lists (IList). The concrete types yielded are JsonObject and JsonArray if that's important to you.
>
> Given your root oJsonObject, you can access the various pieces like this...
>
> Get the logon member of the root object:
>
> Dim logon As String = oJsonObject("logon")
>
> Get the list of addresses from the root object:
>
> Dim addresses As IList = oJsonObject("addresses")
>
> Get the first address object:
>
> Dim address As IDictionary = addresses(0)

Here I tried to do this:

Response.Write(address ("firstName"))

It was equal to "Nothing" and did not output any value.


> Loop through each shipping address object and dump members:
>
> For Each shipping In address("shipping")
> Console.WriteLine(shipping("firstName"))
> Console.WriteLine(shipping("lastName"))
> ' etc.
> End

I received the following error at this point:

"Conversion from string "shipping" to type 'Integer' is not valid."

Here is my full code:
---------------------------------------------------------------------------------------------------------------------------------
Dim oTextReader As System.IO.TextReader
oTextReader = New IO.StreamReader("C:\Documents and Settings
\rtaylor\Desktop\json.txt")

Dim oJsonTextReader As New JsonTextReader(oTextReader)

Dim strString As String = String.Empty
Dim oJsonObject As New JsonObject
Dim oAddress As IDictionary

With oJsonObject
.Import(oJsonTextReader)

If .HasMembers Then
Response.Write("LogonID: " & .Item("logonId").ToString
& "<br>")
Response.Write("First Name: "
& .Item("firstName").ToString & "<br>")

Dim oAddresses As IList = oJsonObject("addresses")

oAddress = oAddresses(0)
Response.Write(oAddress("firstName"))

'Loop through each shipping address object and dump
members:
For Each oAddress In oAddresses("shipping")
Response.Write(oAddress("firstName"))
Response.Write(oAddress("lastName"))
Next
End If
End With
---------------------------------------------------------------------------------------------------------------------------------

Atif Aziz

unread,
Aug 12, 2008, 9:40:31 AM8/12/08
to jay...@googlegroups.com
> BTW, I'm in .net 2.0 and using JayRock version 0.9.8316

That's a perfectly valid and supported configuration. :)

-----Original Message-----
From: jay...@googlegroups.com [mailto:jay...@googlegroups.com] On Behalf Of Toolma...@gmail.com
Sent: Tuesday, August 12, 2008 3:39 PM
To: Jayrock

Toolma...@gmail.com

unread,
Aug 12, 2008, 11:09:58 AM8/12/08
to Jayrock
Any idea on why I'm getting the errors that I am?

I thought maybe the version I'm using didn't support some of the
coding techniques you had suggested.

Atif Aziz

unread,
Aug 12, 2008, 11:39:41 AM8/12/08
to jay...@googlegroups.com
> Any idea on why I'm getting the errors that I am?

Ah, my fault. Didn't scroll down read the rest.

I was doing it off the top of my head so I may have made some mistake. Just tried running the following code on my end with your data and it works fine. I also attached it as a zip to this post. In case it doesn't make it through the filters, you can also find it at http://gist.github.com/5056.

Module MainModule

Sub Main()

Dim root As IDictionary = JsonConvert.Import(File.ReadAllText("json.txt"))

Dim logon As String = root("logon")
Console.WriteLine("logon: " & logon)

Dim addresses As IList = root("addresses")
For Each address As IDictionary In addresses
For Each shipping As IDictionary In address("shipping")
Console.WriteLine()
Console.WriteLine("firstName: " & shipping("firstName"))
Console.WriteLine("lastName : " & shipping("lastName"))
Console.WriteLine("address1 : " & shipping("address1"))
Console.WriteLine("address2 : " & shipping("address2"))
Console.WriteLine("city : " & shipping("city"))
Console.WriteLine("state : " & shipping("state"))
Console.WriteLine("zip : " & shipping("zip"))
Next
Next

End Sub

End Module

Let me know if it worked.

- Atif
test.zip.txt

Toolma...@gmail.com

unread,
Aug 12, 2008, 3:56:02 PM8/12/08
to Jayrock
That works very nice. Thank you. Could I bother you to show me how to
get the billing and marketing address?


On Aug 12, 10:39 am, Atif Aziz <Atif.A...@skybow.com> wrote:
> > Any idea on why I'm getting the errors that I am?
>
> Ah, my fault. Didn't scroll down read the rest.
>
> I was doing it off the top of my head so I may have made some mistake. Just tried running the following code on my end with your data and it works fine. I also attached it as a zip to this post. In case it doesn't make it through the filters, you can also find it athttp://gist.github.com/5056.
> [test.zip.txt]This attachment was removed.

Toolma...@gmail.com

unread,
Aug 13, 2008, 10:22:36 AM8/13/08
to Jayrock

Atif,

Here is how I pulled out the Billing address. Is this the appropriate/
best/suggested way to do this?

--------------------------------------------------------------------------------------------------------------------------------------------
For Each billing As DictionaryEntry In address("billing")
Select Case billing.Key
Case "firstName"
Response.Write("firstName: " & billing.Value &
"<BR>")
Case "lastName"
Response.Write("lastName : " & billing.Value &
"<BR>")
Case "address1"
Response.Write("address1 : " & billing.Value &
"<BR>")
Case "address2"
Response.Write("address2 : " & billing.Value &
"<BR>")
Case "city"
Response.Write("city : " & billing.Value &
"<BR>")
Case "state"
Response.Write("state : " & billing.Value &
"<BR>")
Case "zip"
Response.Write("zip : " & billing.Value &
"<BR>")
End Select
Next
--------------------------------------------------------------------------------------------------------------------------------------------

Atif Aziz

unread,
Aug 13, 2008, 12:10:35 PM8/13/08
to jay...@googlegroups.com
> Here is how I pulled out the Billing address. Is this the appropriate/
> best/suggested way to do this?

Yes, that's one way to do it, but you could have avoided the Select Case switch table and indexed into the dictionary directly like this:

Dim billing As IDictionary = address("billing")
Console.WriteLine("firstName: " & billing("firstName"))
Console.WriteLine("lastName : " & billing("lastName"))
Console.WriteLine("address1 : " & billing("address1"))
Console.WriteLine("address2 : " & billing("address2"))
Console.WriteLine("city : " & billing("city"))
Console.WriteLine("state : " & billing("state"))
Console.WriteLine("zip : " & billing("zip"))

See modified version of earlier code:
http://gist.github.com/5249

- Atif

-----Original Message-----
From: jay...@googlegroups.com [mailto:jay...@googlegroups.com] On Behalf Of Toolma...@gmail.com
Sent: Wednesday, August 13, 2008 4:23 PM
To: Jayrock
Subject: [Jayrock] Re: Best method - vb.net and reading a JSON response



Toolma...@gmail.com

unread,
Aug 13, 2008, 1:58:58 PM8/13/08
to Jayrock
Worked perfect.. thanks so much Atif!

Reply all
Reply to author
Forward
0 new messages