2

I'm using VB in ASP.NET and I've been looking at trying to deserialize the below JSON for about 4 days now with no success.

The problem is the code I have below is returning null values. I would like to get results of the JSON for each class member I've declared including price and shipping values for each product.

Can anyone point me in the right direction with regard to

1.) why I keep getting null values back and

2.) if the classes I declared are valid for the json I am trying to deserialize?

Any help is greatly appreciated!

Here is an example of the JSON I am working with:

{
 "kind": "shopping#products",
 "items": 
   [{
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/8040/8382012077897342942",
   "product": {
    "googleId": "8382012077897342942",
    "title": "LEGO Star Wars™: Jabba's Palace™ (9516)",
    "description": "Rescue Han Solo from Jabba the Hutt's desert palace!",
    "inventories": [
     {
      "price": 119.99,
      "shipping": 12.95,
      "currency": "USD"
     }
    ]
   }
  }
 ]
}

Here is the Code I have currently:

Imports System.Web.Script.Serialization
Imports Newtonsoft.Json.Linq

Public Class inventories
    Public Property price As Double
    Public Property shipping As Double
End Class

Public Class product
    Public Property googleid As String
    Public Property title As String
    Public Inventories As inventories()
End Class

Public Class Items
    Public Property product As product()
    Public Property kind As String
    Public Property id As String
End Class


Partial Class JSON_Test
    Inherits System.Web.UI.Page

    Protected Sub getbutton_Click(sender As Object, e As System.EventArgs) Handles getbutton.Click

        ' 1. Get JSON string from Google
        Dim google_json_string As String
        google_json_string = json_text.Text


        '2. Deserialize JSON - Method 1
        Dim jss As New JavaScriptSerializer
        Dim ds_results = jss.Deserialize(Of List(Of Items))(google_json_string)
        result1.Text = ds_results.Count
        result2.Text = ds_results(0).kind

    End Sub
End Class

(Update) I've updated the code to include classes that are structured like this (thanks Dan-o):

Public Class g_JSON
    Public Property items As List(Of Items)
End Class

Public Class Items
    Public Property product As product()
    Public Property kind As String
    Public Property id As String
End Class

Public Class product
    Public Property googleid As String
    Public Property title As String
    Public Inventories As inventories()
End Class

Public Class inventories
    Public Property price As Double
    Public Property shipping As Double
End Class

I also updated the code to read as follows:

Partial Class JSON_Test
    Inherits System.Web.UI.Page

    Protected Sub getbutton_Click(sender As Object, e As System.EventArgs) Handles getbutton.Click

        ' 1. Get JSON string from Google
        Dim google_json_string As String
        google_json_string = json_text.Text


        '2. Deserialize JSON - Method 1
        Dim jss As New JavaScriptSerializer
        Dim ds_results = jss.Deserialize(Of g_JSON)(google_json_string)
        result1.Text = ds_results.items(0).id
        result2.Text = ds_results.items(0).kind

    End Sub
End Class

Now, the code will compile without issue but when I kick off the click event I get the following error:

No parameterless constructor defined for type of 'product[]'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for type of 'product[]'.

Source Error: Line 38: Dim ds_results = jss.Deserialize(Of g_JSON)(google_json_string)

What does that mean and how do I create a parameterless constructor for product()?

Thanks for taking a look!

0

2 Answers 2

2

According to jsonlint your json isn't valid (comma after "inventories" array).

Sign up to request clarification or add additional context in comments.

3 Comments

Good spot! Did you use a tool to find this?
Yes, it's linked in the comment.
Yes, thank you very much for answering. I pared down the original JSON from 85 items to 1 item to paste an example into this question and forgot to remove the additional comma. I've updated the text with the removed comma. Also, thanks for the reference to the site for validating the JSONs!
1

You forgot the container.. the class that holds everything...

Class something
  Public property kind as string = String.Empty

  Public property items as list(of Item) = Nothing
End Class

Then you deserialize to something, not list(of item)

4 Comments

Dan-o, this is a great suggestion and I've implemented it in the classes and code.
Dan-o, thanks for taking the time to answer. This is a great suggestion and I've implemented it in the classes and code as seen in my edited comments above. I now get the following error " when I kick of the code with a button: "No parameterless constructor defined for type of 'product[]'". What does that mean? At least I am getting farther along (I think?).
Just what the error says. The deserializer is looking for a parameterless constructor for your product array (Items.product). You need to change your array into a List(Of Product), which would have a parameterless ctor.
Thanks for the help! That worked to remove the error and I am plugging along.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.