Can't wrap my head around reading JSON

I have an API response in JSON structure as follows:

{“items”:[
{“id”:“1001”,“productName”:“Product 1”,“quantity”:1},
{“id”:“1002”,“productName”:“Product 2”,“quantity”:23},
{“id”:“1003”,“productName”:“Product 3”,“quantity”:446}]}

I want to extract the productname and quantity from id:1002.
I get as far as:
Var json As New JSONItem
Var s As string
Try
json.Load(content)
Catch error As JSONException
MessageBox("JSON Error: “+error.ErrorNumber.ToString+” "+error.Message)
end
s = JSONItem(json.Value(“items”)…

and can’t figure out where to go from here.
Shake your head, roll your eyes, pity me, but throw me a bone here and help me over this learning threshhold.

“items” is an array of JSONitems:

  Dim response As New JSONItem(TheJSONResultAsString)
  Dim itemarray As JSONItem = response.Value("items")
  
  For i As Integer = 0 To itemarray.Count - 1
    Dim item As JSONItem = itemarray.Child(i)
    
    Dim id As Integer = item.Value("id")
    Dim nm As String = item.Value("productName")
    Dim qt As Integer = item.Value("quantity")
    
  Next
3 Likes

So, using this as a base I was able to get all the data I needed.
On top of that, I’m a big step forward in understanding how to make Xojo and JSON play nicely together.
Thirdly, I get to sleep tonight.
Thanks three times over. :slight_smile:

1 Like