Results 1 to 9 of 9

Thread: Help Deserialize JSON string into list of objects?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    26

    Help Deserialize JSON string into list of objects?

    Hi,

    I am a novice vb.net programmer. I am trying to deserialize a JSON string into a LIST of objects.

    Here is the json...

    Code:
    {
       "categories":[
          {
             "id":"16310161",
             "name":"Industrial & Scientific",
             "has_children":true,
             "is_root":true,
             "path":"Industrial & Scientific",
             "link":"https://www.amazon.com/s?node=16310161",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"301668",
             "name":"CDs & Vinyl",
             "has_children":true,
             "is_root":true,
             "path":"CDs & Vinyl",
             "link":"https://www.amazon.com/s?node=301668",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"10677470011",
             "name":"Vehicles",
             "has_children":false,
             "is_root":true,
             "path":"Vehicles",
             "link":"https://www.amazon.com/s?node=10677470011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"4991426011",
             "name":"Collectibles & Fine Arts",
             "has_children":true,
             "is_root":true,
             "path":"Collectibles & Fine Arts",
             "link":"https://www.amazon.com/s?node=4991426011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"165795011",
             "name":"Toys & Games",
             "has_children":true,
             "is_root":true,
             "path":"Toys & Games",
             "link":"https://www.amazon.com/s?node=165795011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"2335753011",
             "name":"Cell Phones & Accessories",
             "has_children":false,
             "is_root":true,
             "path":"Cell Phones & Accessories",
             "link":"https://www.amazon.com/s?node=2335753011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"11846801",
             "name":"Video Games",
             "has_children":true,
             "is_root":true,
             "path":"Video Games",
             "link":"https://www.amazon.com/s?node=11846801",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"11965861",
             "name":"Musical Instruments",
             "has_children":true,
             "is_root":true,
             "path":"Musical Instruments",
             "link":"https://www.amazon.com/s?node=11965861",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"3375301",
             "name":"Sports & Outdoors",
             "has_children":true,
             "is_root":true,
             "path":"Sports & Outdoors",
             "link":"https://www.amazon.com/s?node=3375301",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"468240",
             "name":"Tools & Home Improvement",
             "has_children":true,
             "is_root":true,
             "path":"Tools & Home Improvement",
             "link":"https://www.amazon.com/s?node=468240",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"2619526011",
             "name":"Appliances",
             "has_children":true,
             "is_root":true,
             "path":"Appliances",
             "link":"https://www.amazon.com/s?node=2619526011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"15690151",
             "name":"Automotive",
             "has_children":true,
             "is_root":true,
             "path":"Automotive",
             "link":"https://www.amazon.com/s?node=15690151",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"3760931",
             "name":"Health & Personal Care",
             "has_children":true,
             "is_root":true,
             "path":"Health & Personal Care",
             "link":"https://www.amazon.com/s?node=3760931",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"1084128",
             "name":"Office Products",
             "has_children":true,
             "is_root":true,
             "path":"Office Products",
             "link":"https://www.amazon.com/s?node=1084128",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"2864120011",
             "name":"Gift Cards",
             "has_children":true,
             "is_root":true,
             "path":"Gift Cards",
             "link":"https://www.amazon.com/s?node=2864120011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"409488",
             "name":"Software",
             "has_children":true,
             "is_root":true,
             "path":"Software",
             "link":"https://www.amazon.com/s?node=409488",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"1063498",
             "name":"Home & Kitchen",
             "has_children":true,
             "is_root":true,
             "path":"Home & Kitchen",
             "link":"https://www.amazon.com/s?node=1063498",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"2617942011",
             "name":"Arts, Crafts & Sewing",
             "has_children":true,
             "is_root":true,
             "path":"Arts, Crafts & Sewing",
             "link":"https://www.amazon.com/s?node=2617942011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"3238155011",
             "name":"Patio, Lawn & Garden",
             "has_children":true,
             "is_root":true,
             "path":"Patio, Lawn & Garden",
             "link":"https://www.amazon.com/s?node=3238155011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"2858778011",
             "name":"Amazon Instant Video",
             "has_children":true,
             "is_root":true,
             "path":"Amazon Instant Video",
             "link":"https://www.amazon.com/s?node=2858778011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"493964",
             "name":"Electronics",
             "has_children":true,
             "is_root":true,
             "path":"Electronics",
             "link":"https://www.amazon.com/s?node=493964",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"624868011",
             "name":"Digital Music",
             "has_children":true,
             "is_root":true,
             "path":"Digital Music",
             "link":"https://www.amazon.com/s?node=624868011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"11260433011",
             "name":"Handmade",
             "has_children":true,
             "is_root":true,
             "path":"Handmade",
             "link":"https://www.amazon.com/s?node=11260433011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"165797011",
             "name":"Baby",
             "has_children":true,
             "is_root":true,
             "path":"Baby",
             "link":"https://www.amazon.com/s?node=165797011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"599872",
             "name":"Magazine Subscriptions",
             "has_children":true,
             "is_root":true,
             "path":"Magazine Subscriptions",
             "link":"https://www.amazon.com/s?node=599872",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"2625374011",
             "name":"Movies & TV",
             "has_children":true,
             "is_root":true,
             "path":"Movies & TV",
             "link":"https://www.amazon.com/s?node=2625374011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"16310211",
             "name":"Grocery & Gourmet Food",
             "has_children":true,
             "is_root":true,
             "path":"Grocery & Gourmet Food",
             "link":"https://www.amazon.com/s?node=16310211",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"1000",
             "name":"Books",
             "has_children":true,
             "is_root":true,
             "path":"Books",
             "link":"https://www.amazon.com/s?node=1000",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"133141011",
             "name":"Kindle Store",
             "has_children":true,
             "is_root":true,
             "path":"Kindle Store",
             "link":"https://www.amazon.com/s?node=133141011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"11055981",
             "name":"Beauty",
             "has_children":true,
             "is_root":true,
             "path":"Beauty",
             "link":"https://www.amazon.com/s?node=11055981",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"7141124011",
             "name":"Clothing, Shoes & Jewelry",
             "has_children":true,
             "is_root":true,
             "path":"Clothing, Shoes & Jewelry",
             "link":"https://www.amazon.com/s?node=7141124011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"2350150011",
             "name":"Apps & Games",
             "has_children":true,
             "is_root":true,
             "path":"Apps & Games",
             "link":"https://www.amazon.com/s?node=2350150011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"2619534011",
             "name":"Pet Supplies",
             "has_children":true,
             "is_root":true,
             "path":"Pet Supplies",
             "link":"https://www.amazon.com/s?node=2619534011",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"audible",
             "name":"Audible",
             "has_children":false,
             "is_root":true,
             "path":"Audible",
             "link":"https://www.amazon.com/s?node=audible",
             "type":"standard",
             "domain":"amazon.com"
          },
          {
             "id":"amazonfresh",
             "name":"Amazon Fresh",
             "has_children":false,
             "is_root":true,
             "path":"Amazon Fresh",
             "link":"https://www.amazon.com/s?node=amazonfresh",
             "type":"standard",
             "domain":"amazon.com"
          }
       ]
    }

    And I am using newtonsoft to deserialize. I am confused on how this even works. All I would like is a simple way to organize this info and read fields from a list. For instance, I need all the IDs, or names... I don't necessarily need them to be placed into an object, although thats probably the best way.


    So here is my object to deserialize into....

    Code:
    Public Class CategoryContainer
            Public ID As String
            Public Type As String
            Public Name As String
            Public HasChildren As Boolean
            Public IsRoot As Boolean
            Public Path As String
            Public Link As String
        End Class

    And here is how I attempt to deserialize....
    Code:
    Dim cat = JsonConvert.DeserializeObject(Of List(Of CategoryContainer))(str)  'str = json string
    And the failure is...

    Code:
    Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Project1.Module3+CategoryContainer]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Help Deserialize JSON string into list of objects?

    Try this...

    Code:
    Public Class Rootobject
        Public Property categories As List(Of Category)
    End Class
    
    Public Class Category
        Public Property id As String
        Public Property name As String
        Public Property has_children As Boolean
        Public Property is_root As Boolean
        Public Property path As String
        Public Property link As String
        Public Property type As String
        Public Property domain As String
    End Class
    Code:
    Imports Newtonsoft.Json
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim str As String = IO.File.ReadAllText("json.txt")
            Dim cat = JsonConvert.DeserializeObject(Of Rootobject)(str)
            Stop
        End Sub
    End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    26

    Re: Help Deserialize JSON string into list of objects?

    Paul,

    Thanks for your help.

    Unfortunately this did not solve the problem.

    Here is code snippets...

    Code:
       Public Class CategoryContainer
            Public id As String
            Public type As String
            Public name As String
            Public has_children As Boolean
            Public is_root As Boolean
            Public path As String
            Public link As String
            Public domain As String
        End Class
    
        Public Class CategoryContainerList
            Public Property ContainerList As List(Of CategoryContainer)
        End Class


    Code:
    Dim cat = JsonConvert.DeserializeObject(Of CategoryContainerList)(str)
    Debug.Print(cat.ContainerList.ToString)



    And the deserialization apparently returns nothing?.....
    Code:
    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    
    Project1.Module3.CategoryContainerList.ContainerList.get returned Nothing.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    26

    Re: Help Deserialize JSON string into list of objects?

    Update:

    I changed the root (list) classes' property 'ContainerList' to match the JSON file 'categories' and the solution worked!

    Didn't think that would matter. Thanks so much for your help!!

    Code:
        Public Class CategoryContainer
            Public Property id As String
            Public Property name As String
            Public Property has_children As Boolean
            Public Property is_root As Boolean
            Public Property link As String
            Public Property path As String
            Public Property type As String
            Public Property domain As String
        End Class
    
        Public Class CategoryContainerList
            Public Property categories As List(Of CategoryContainer) 'this property name must match json array name 'categories' !
        End Class

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Help Deserialize JSON string into list of objects?

    Try this...

    Code:
    Public Class CategoryContainerList
        @JsonProperty("categories")
        Public Property ContainerList As List(Of CategoryContainer) 
    End Class

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Help Deserialize JSON string into list of objects?

    Here's the correct syntax for VB...

    Code:
    Imports Newtonsoft.Json
    
    Public Class CategoryContainerList
        <JsonProperty("categories")>
        Public Property ContainerList As List(Of CategoryContainer)
    End Class
    
    Public Class CategoryContainer
        Public Property id As String
        Public Property name As String
        Public Property has_children As Boolean
        Public Property is_root As Boolean
        Public Property path As String
        Public Property link As String
        Public Property type As String
        Public Property domain As String
    End Class
    Code:
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim str As String = IO.File.ReadAllText("json.txt")
            Dim cat = JsonConvert.DeserializeObject(Of CategoryContainerList)(str)
            Stop
        End Sub
    End Class

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    26

    Re: Help Deserialize JSON string into list of objects?

    Paul,

    I tried adding the <json property> tag above the CategoryContainer class properties originally.

    Thanks for adding that in there. Also, what is that line of code called, the json property tags?

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Help Deserialize JSON string into list of objects?

    They're called annotations.

    That one is specifically called a json property annotation.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Help Deserialize JSON string into list of objects?

    Quote Originally Posted by BitFlipper88 View Post
    Paul,

    I tried adding the <json property> tag above the CategoryContainer class properties originally.

    Thanks for adding that in there. Also, what is that line of code called, the json property tags?
    As tg told you…
    The class structure layout works with just one json property annotation because all the other properties have the same names as the properties in the json file. The class names aren’t important.

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