Trying to deserialise the following json returned from a web source:
{
"cards": [
{
"high": "8.27",
"volume": 5,
"percent_change": "0.00",
"name": "Wurmcoil engine",
"url": "http://blacklotusproject.com/cards/Scars+of+Mirrodin/Wurmcoil+Engine/",
"price": "6.81",
"set_code": "SOM",
"average": "5.67",
"change": "0.00",
"low": "1.12"}],
"currency": "USD"
}
I am using json.net with visual basic, new to both of them, especially the object oriented portions of vb. I would just like to extract the ‘price’ variable.
I have set up a class as such:
Public Class Card
Public high As String
Public volume As String
Public percent_change As String
Public name As String
Public url As String
Public price As String
Public set_code As String
Public average As String
Public change As String
Public low As String
End Class
The code I am currently using is:
Public Sub parse_json(url As String)
Dim blp_json As String = ""
Dim wClient As New WebClient
wClient.Proxy = System.Net.HttpWebRequest.DefaultWebProxy
blp_json = wClient.DownloadString(url)
MessageBox.Show(blp_json)
Dim card_1 = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Card)(blp_json)
PriceTextBox.Text = card_1.price
TextBox1.AppendText(card_1.ToString)
TextBox1.AppendText(blp_json)
End Sub
Just trying many different things to get a hang of it, not really sure what I am doing. I presume my Card class is incorrect as ‘price’ appears to be nested in cards:[{…}]
I don’t really know about deserialising json at all, and much less about how to do it /properly/ in vb.
I use the
System.Web.Script.Serialization.JavaScriptSerializerfor JSON deserialization. Your example is slightly complicated by the fact thatcardscontains an array of JSON objects. This is indicated by the “[” and “]”. This example code will show you how to process it regardless of whethercardsis an array or not. You may wish to ignore theElseif you are sure that there will always be an array incardsMake sure that you have included a reference to System.Web.Extensions in your project and…
and then…