Reputation: 37
As standard, I've created a web request and receive a response as JSON format. I'm trying to deserialize this JSON using JSON.NET (I don't think I need this though).
I've tried using the following code, however I'm not entirely sure on how to make the object actually contain some data. When I run this code, I get an error message displaying that my JObject "current" is "Nothing".
Imports System.Net
Imports System.IO
Imports Newtonsoft.Json.Linq
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
ServicePointManager.DefaultConnectionLimit = 9999
Dim uriString As String = "https://dev.tescolabs.com/grocery/products/?query=chicken&offset=0&limit=2"
Dim uri As New Uri(uriString)
Dim r As HttpWebRequest = HttpWebRequest.Create(uri)
r.Headers("Ocp-Apim-Subscription-Key") = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
r.Method = "GET"
r.Proxy = Nothing
Dim re As HttpWebResponse = r.GetResponse()
Dim read As New StreamReader(re.GetResponseStream())
Dim raw As String = read.ReadToEnd()
Dim a As JObject = JObject.Parse(raw)
Dim current As JObject = DirectCast(a("image"), JObject)
MessageBox.Show(current("image"))
End Sub
End Class
Public Class Totals
Public Property all As Integer
Public Property _new As Integer
Public Property offer As Integer
End Class
Public Class Result
Public Property image As String
Public Property superDepartment As String
Public Property tpnb As Integer
Public Property ContentsMeasureType As String
Public Property name As String
Public Property UnitOfSale As Integer
Public Property description() As String
Public Property AverageSellingUnitWeight As Single
Public Property UnitQuantity As String
Public Property id As Integer
Public Property ContentsQuantity As Single
Public Property department As String
Public Property price As Single
Public Property unitprice As Single
End Class
So, in textbox1, should be each product, including all of the information for each product. After extracting all of this information, I would ultimately like to add the information for each product in a datagridview, to present the information in a more clear manner. However, I can't get past this stage.
I have now tried the following code:
Dim results = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Result)(raw)
For Each image In results.image
TextBox1.Text = "Image URL:" + results.image
Next
JSON I receive as response:
{
"uk" : {
"ghs" : {
"products" : {
"input_query" : "chicken",
"output_query" : "chicken",
"filters" : { },
"queryPhase" : "primary",
"totals" : {
"all" : 1358,
"new" : 9,
"offer" : 478
},
"config" : "default",
"results" : [ {
"image" : "http://img.tesco.com/Groceries/pi/325/5057008546325/IDShot_90x90.jpg",
"superDepartment" : "Fresh Food",
"tpnb" : 81866107,
"ContentsMeasureType" : "G",
"name" : "Tesco British Chicken Breast Portions 650G",
"UnitOfSale" : 1,
"description" : [ "Fresh class A skinless chicken breast fillet portions."],
"AverageSellingUnitWeight" : 0.746,
"UnitQuantity" : "KG",
"id" : 294007923,
"ContentsQuantity" : 650,
"department" : "Fresh Meat & Poultry",
"price" : 3.8,
"unitprice" : 5.85
}, {
"image" : "http://img.tesco.com/Groceries/pi/531/5054775703531/IDShot_90x90.jpg",
"superDepartment" : "Fresh Food",
"tpnb" : 64083120,
"ContentsMeasureType" : "KG",
"name" : "Tesco British Large Whole Chicken 1.55-1.95Kg",
"UnitOfSale" : 1,
"AverageSellingUnitWeight" : 1.785,
"description" : [ "Fresh Class A whole chicken without giblets."],
"UnitQuantity" : "KG",
"id" : 292276232,
"ContentsQuantity" : 1.75,
"department" : "Fresh Meat & Poultry",
"price" : 3.5,
"unitprice" : 2.0
} ],
"suggestions" : [ ]
}
}
}
}
However I still don't receive the image URL in textbox1, and have no idea as to why. Any help would be greatly appreciated.
Upvotes: 0
Views: 65
Reputation: 884
I've never used JSON.NET
or NEWTONSOFT
, I tend to only mess around with JSON a little, I usually just use the 'built in' method.
Your results are in an array which is what your first problem would probably have been. Then your For Each
looks like it's getting on the right track but not sure results
are being referenced correctly?
Anyway...
He's a working example that will hopefully help.
I just made a simple WinForm and added a button
.
I added a reference to: System.Web.Extensions
Try code below: (let me know how you got on)
Imports System.Web.Script.Serialization
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'This is just my JSON source I used for testing. (The JSON response you posted)
Dim raw As String = IO.File.ReadAllText("C:\MEDIA\json_test.json")
'This should now be the same as your: Dim raw As String = read.ReadToEnd()
'From here on, try this:
'Deserialise
Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
Dim Tesco As JSON = New JSON
Tesco = ser.Deserialize(Of JSON)(raw)
'Loop through results and print each image URL
For Each r As Result In Tesco.uk.ghs.products.results
Console.WriteLine("Image URL:" & r.image)
Next
End Sub
End Class
Public Class Totals
Public Property all As Integer
Public Property [new] As Integer
Public Property offer As Integer
End Class
Public Class Result
Public Property image As String
Public Property superDepartment As String
Public Property tpnb As Integer
Public Property ContentsMeasureType As String
Public Property name As String
Public Property UnitOfSale As Integer
Public Property description As String()
Public Property AverageSellingUnitWeight As Double
Public Property UnitQuantity As String
Public Property id As Integer
Public Property ContentsQuantity As Double
Public Property department As String
Public Property price As Double
Public Property unitprice As Double
End Class
Public Class Products
Public Property input_query As String
Public Property output_query As String
Public Property queryPhase As String
Public Property totals As Totals
Public Property config As String
Public Property results As Result()
Public Property suggestions As Object()
End Class
Public Class Ghs
Public Property products As Products
End Class
Public Class Uk
Public Property ghs As Ghs
End Class
Public Class JSON
Public Property uk As Uk
End Class
Upvotes: 1