0

Has anyone an idea how to loop JSON string and get all data by JSON key? I have very heavy JSON file with arrays in arrays in array.

{
  "ID":"33",
  "A":a,
  "SUB-ID": [
    { "ID":"33"},
    { "A":"b"},
    { "SUB-ID":[] }
  ]
"ID":"37",
  "A":a,
  "SUB-ID": [
    { "ID":"38"},
    { "A":"b"},
    { "SUB-ID":[] }
  ]
"ID":"39",
  "A":a,
  "SUB-ID": [
    { "ID":"31"},
    { "A":"b"},
    { "SUB-ID":["ID":"30",SUB-ID[...]] } 'And this array in array "Sub-ID" - there may be more below each other 
  ]
 } 

In Sub-Id can be infinite loop arrays....

This JSON can have lot of sub-arrays... And i need to get list of all IDs.

Cz Raven
  • 33
  • 8
  • You don't *loop a JSON string*, you deserialize the JSON. This doesn't look like a valid JSON. – Jimi Feb 26 '21 at 08:24
  • This is only example of "my" JSON file - my problem is in infinite loops... – Cz Raven Feb 26 '21 at 09:03
  • Can you post the actual JSON? What generates it? Is it an API response? Can you post the address in that case, so it can be tested? -- What does *infinite loops* means here? Did you try to deserialize it and the serializer throws an exception? If so, what serializer? Do you have coded something? – Jimi Feb 26 '21 at 11:06
  • 1
    Do the answers to [How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?](https://stackoverflow.com/questions/5546142/how-do-i-use-json-net-to-deserialize-into-nested-recursive-dictionary-and-list) help? – Andrew Morton Feb 26 '21 at 11:20
  • The JSON you posted in your question in invalid. Can you include a valid sample? You can use https://jsonlint.com/ to check whether JSON is valid. – Brian Rogers Mar 26 '21 at 19:49

1 Answers1

0

You do not need to loop through aka manually parse json text to create your object. Go to your nugent packages and download Newtonsoft json and add it to your project.

Now if you have a class written with that object You can now call the following:

Dim myJsonText as string = (your json text)

Dim myJsonObject as new (whatever your object name is)

MyJsonObject = JsonConvert.DeserializeObject(myJsonText, GetType(yourobjectName))

It will now create your object and now you can assign your object properties like this.

MyJsonObject.ID = 3
MyJsonObject.A=“A”

If you need to convert it back to json text you call the following:

MyJsonText = jsonConvert.SERIALIZEObject(myJsonObject)
Stavros
  • 23
  • 1
  • 5