Search code examples
jsonvb.netjson.net.net-3.5

How to cycle for dynamic JObjects in another JObject?


Actually i'm am processing some JSON data get from some API in an old VB.NET project in Framework 3.5

I'm stuck in the processing of the following response:

{
  "data": {
    "89359": {
      "name": "A",
      //...
    },
    "89360": {
      "name": "B",
      //...
    }
  }
}

Each item in data is dynamic but the API doesn't set the data as an array, i was trying to get the data from each item as i was yet doing for JSON arrays like this:

Dim options As JObject = JObject.Parse(json)
For Each opt As JObject In options("data")
    MsgBox(opt("name"))
Next

But it's not working in this case as as i said data is not an array...

So how could process data from that JSON as 89359 and 89360 are dynamic?


Solution

  • Your problem is that options("data") is declared to be of type JToken while actually being of type JObject, so when you enumerate it you are actually enumerating its JProperty children. But what you need are the values of those JProperty objects which are one level deeper in the hierarchy:

    For Each jProperty As JProperty In options("data")
        Dim propertyName = jProperty.Name ' The "89359" names, in case you need them for some reason
        Dim opt As JObject = jProperty.Value
        Dim name As String = opt("name")  ' Cast the name to a String using the JToken implicit casting operator
        ' Use the name as required
        MsgBox(name)
    Next    
    

    Demo fiddle here.