Problems with JSON response

HI all,

Please could you help point out where I’m going wrong with my JSON response? My error is on assigning my parsed json data to the templates variable and the debugger says: ‘Expected array of Auto but got class Xojo.Core.Dictionary’ which I can understand, but changing my templates variable to dictionary doesn’t help. Is it because my json data contains an array of json objects?

Sub PageReceived(URL as Text, HTTPStatus as Integer, Content as xojo.Core.MemoryBlock)
Dim jsonData as Text

// Convert memorblock data into text
jsonData = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(content)

// Parse json data into array
Dim templates() As Auto
templates = Xojo.Data.ParseJSON(jsonData)

// For each post in the array, add the title to the listbox
For Each subDict As Xojo.Core.Dictionary in templates
  Dim t As String = subDict.Value("templateId")
  Listbox1.AddRow(t)
Next
End Sub

The JSON response I’m getting from the server is:

{
   "templates":[
      {
         "templateId":"IDCR Allergies List.v0",
         "createdOn":"2017-11-17T15:01:42.253Z"
      },
      {
         "templateId":"IDCR - Laboratory Order.v0",
         "createdOn":"2017-11-17T15:01:42.368Z"
      },
      {
         "templateId":"IDCR - Laboratory Test Report.v0",
         "createdOn":"2017-11-17T15:01:42.293Z"
      },
      {
         "templateId":"IDCR Problem List.v1",
         "createdOn":"2017-11-17T15:01:42.221Z"
      },
      {
         "templateId":"IDCR Procedures List.v0",
         "createdOn":"2017-11-17T15:01:42.562Z"
      },
      {
         "templateId":"Vital Signs Encounter (Composition)",
         "createdOn":"2017-11-17T15:01:42.477Z"
      }
   ]
}

Thank you for any help and hints.

I see no error, you could try;

To change

  Dim t As String = subDict.Value("templateId")

to

  Dim t As Text = subDict.Value("templateId")

At the top layer, this JSON is an object (Dictionary) with one key/value pair. Try this:

// Parse json data into array
Dim json As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(jsonData)
Dim templates() As Auto = json.Value("templates")

Thanks both.