Results 1 to 40 of 65

Thread: Help using JSON API that requires Authentication with JWT token

Threaded View

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

    Re: Help using JSON API that requires Authentication with JWT token

    OK. This is a case of their API documentation not being 100% accurate. Welcome to development, this happens sometimes.

    If you look at JSON as "a string", you will have a hard time interpreting it. You have to understand what the symbols mean. There's not many symbols and rules to remember:
    • [] means an array. Arrays can contain values or objects, separated by commas.
    • {} means an object. Objects can contain properties, separated by commas.
    • A property has a name, enclosed in quotes, and a value. They are separated by a colon.
    • A value can be:
      • A string, "which must be enclosed in quotes".
      • A number, which doesn't have quotes.
      • An array.
      • An object.


    Building a VB object out of a JSON object is tough, compared to some other languages, because VB is very verbose and you can't really nest object declarations inside each other. When I read JSON, I tend to defer nested things until I can see the whole picture. Let's take a look at the JSON for /series/{id}. I won't copy/paste it here, for brevity, but I will show the parts I'm looking at.

    This API call returns an object with one property: "data". That property is an array that contains other objects:
    Code:
    {
        "data" : [{ ... }, ... ]
    }
    I want to call this type "SeriesSearchResults", since it represents many possible results.

    The one inside "data" looks really familiar:
    Code:
    {
        "added" : "string",
        ...
    }
    I want to call this type "SeriesSearchResult", since it's one result.

    Code:
    Public Class SearchSeriesResults
        Public Property data() As SearchSeriesResult()
    End Class
    
    Public Class SearchSeriesResult
        ... same as before ...
    End Class
    So now you're trying to deserialize:
    Code:
    Dim responseObject = JsonConvert.DeserializeObject(Of SearchSeriesResults)(searchResults)
    That's a heck of a lot more likely to work.

    Note the difference between what you read and what it was: there is a "data" property and THAT contains an array. If the result had been an array alone, it would've looked like:
    Code:
    [ { ... }, { ... } ]
    Especially since we now see their documentation isn't 100% accurate, it's important to know how to read JSON so you can figure out how it corresponds to objects.
    Last edited by Sitten Spynne; Jun 19th, 2017 at 03:41 PM. Reason: New post with factual information!
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Tags for this Thread

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