Reputation: 25
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!
Upvotes: 2
Views: 3289
Reputation: 98
According to jsonlint your json isn't valid (comma after "inventories" array).
Upvotes: 2
Reputation: 33738
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)
Upvotes: 1