Results 1 to 5 of 5

Thread: VB.NET JSON Rest API

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    3

    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

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: VB.NET JSON Rest API

    If Stats object contains the deserialized JSON, the code should be:
    VB.NET Code:
    1. lbl1.Text = Stats.Stat_1
    given that Stat_1 is a property of MyClass.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    3

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    3

    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!

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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
  •  



Click Here to Expand Forum to Full Width