Results 1 to 3 of 3

Thread: Iiterate any json sring

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Iiterate any json sring

    I'm trying to loop through this string but it stops right after the array.

    Here is the JSON String
    Code:
    [
          {
            "@context": "http://schema.org",
            "@type": "BreadcrumbList",
            "itemListElement": [
              {
                "@type": "ListItem",
                "position": 1,
                "item": {
                  "@id": "https://www.site.com/",
                  "name": "Home",
                  "image": null
                }
              },
              {
                "@type": "ListItem",
                "position": 2,
                "item": {
                  "@id": "https://www.site.com/recipes/",
                  "name": "Recipes",
                  "image": null
                }
              },
              {
                "@type": "ListItem",
                "position": 3,
                "item": {
                  "@id": "https://www.site.com/recipes/80/main-dish/",
                  "name": "Main Dish Recipes",
                  "image": null
                }
              },
              {
                "@type": "ListItem",
                "position": 4,
                "item": {
                  "@id": "https://www.site.com/recipes/258/main-dish/roasts/",
                  "name": "Roast Recipes",
                  "image": null
                }
              }
            ]
          },
          {
            "@context": "http://schema.org",
            "@type": "Recipe",
            "mainEntityOfPage": "https://www.site.com/recipe/56352/garlic-prime-rib/",
            "name": "Garlic Prime Rib",
            "image": {
              "@type": "ImageObject",
              "url": "https://imagesvc.site.io/v3/mm/image?url=https%3A%2F%2FFuserphotos%2F6036515.jpg",
              "width": null,
              "height": null,
              "caption": "Garlic Prime Rib"
            }
    ]
    and the code
    Code:
    Dim res As JToken = JToken.Parse(RTBJson.Text)
    Select Case res.Type
                    Case 1
                    Case 2 ' we start with an array
                       
                        CollectItems(res.Item(0))
    end select
     Sub CollectItems(ByVal jToken As JToken)
            
    Select Case jToken.Type
                Case JTokenType.Object
                    MessageBox.Show(jToken.Count.ToString)
                   For Each child In jToken.Children(Of JProperty)()
                        CollectItems(child)
                    Next               
    
                Case JTokenType.Array
    
                    For Each child In jToken.Children().ToList
                        CollectItems(child)
                    Next
    
                Case JTokenType.[Property]               
                    Me.GetCase(jToken)
                    ' Next
            End Select
        End Sub
    messagebox says there is only 3 items. how do I get it to see all of it? What am I missing?

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Iiterate any json sring

    Well i think I fixed my own problem. But it leads to another problem. So instead of sending res.Item.(0) I just send res. It works it out. But when it gets to the image it gives a null value

    Code:
    TxBxImage.Text = item.SelectToken("image.url").ToString
    I was thinking it was an JObject so you call it like that, what did I do wrong?

    DOH! I missed value in there. Should be
    Code:
    TxBxImage.Text = item.Value.SelectToken("url").ToString
    Thanks everybody.

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Iiterate any json sring

    Quote Originally Posted by phpman View Post
    Thanks everybody.
    Errr.....You're welcomed?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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