-
Apr 26th, 2016, 06:14 AM
#1
Thread Starter
New Member
VB.NET JSON Rest API
Hi guys,
Looking for some advice,
Im trying to parse some JSON from a Rest API.
I've successfully Connected using headers, and read the JSON content into a Var.
If i print the Var into a text box i see what i would expect.
I have copied the JSON and Pasted into a new class, so it should be set correctly.
Now i'm trying to save the JSON into the class and call it into
Code:
Dim jsSerializer As New System.Web.Script.Serialization.JavaScriptSerializer
Dim Stats As MyClass = jsSerializer.Deserialize(Of MyClass)(jsonString)
No errors at all, but when i put the values in the label is blank?
Code:
lbl1.Text = MyClass.Stats.Stat_1
Any Ideas?
Thanks in advance
-
Apr 26th, 2016, 10:50 AM
#2
Re: VB.NET JSON Rest API
If Stats object contains the deserialized JSON, the code should be:
given that Stat_1 is a property of MyClass.
-
Apr 27th, 2016, 03:12 AM
#3
Thread Starter
New Member
Re: VB.NET JSON Rest API
Thanks for you reply, i changed the code over. Still no error, and still a blank Label
Seems like im missing something obvious and its slowly driving me insane
-
Apr 27th, 2016, 04:27 AM
#4
Thread Starter
New Member
Re: VB.NET JSON Rest API
Nevermind, i cracked it!
Got rid of the Class all together, didn't overly need it anyway.
In the end i used NewtonSoft
Code:
Dim readingJson = Newtonsoft.Json.Linq.JObject.Parse(jsonString)
lblStat1.Text = (readingJson.Item("stats")("stat1").ToString)
Thanks for your help!
-
Apr 27th, 2016, 09:45 AM
#5
Re: VB.NET JSON Rest API
Ah, I was just coming in this thread to suggest Newtonsoft's API.
The short version of the story is .NET does not ship with a JSON parser capable of interoperating with anything but itself. Someone's going to poke their head in here and say, "But DataContractJsonSerializer!". It doesn't serialize collections/dictionaries in a way that makes sense. Newtonsoft's output is much closer to what people would expect a Dictionary to serialize to.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|