Another json question

Hi guys,
i’ve read lot of threads but i cannot understand how to do this :

I receive a JSON from an API in this format :

{
  "lista_clienti": [
    {
      "id": "111111",
      "nome": "XXXXXX",
      "referente": "",
      "indirizzo_via": "XXXXX",
      "indirizzo_cap": "XXXXXX",
      "indirizzo_citta": "XXXXXX",
      "indirizzo_provincia": "XXXXXX",
      "indirizzo_extra": "",
      "paese": "XXXXXX",
      "mail": "",
      "tel": "",
      "fax": "282",
      "piva": "XXXXXX",
      "cf": "",
      "termini_pagamento": "0",
      "pagamento_fine_mese": false,
      "val_iva_default": "",
      "desc_iva_default": "",
      "extra": "",
      "PA": false
    },
    {
      "id": "1234456",
      "nome": "XXXXXX",
      "referente": "",
      "indirizzo_via": "XXXXXX",
      "indirizzo_cap": "XXXXXX",
      "indirizzo_citta": "XXXXXX",
      "indirizzo_provincia": "XXXXXX",
      "indirizzo_extra": "",
      "paese": "XXXXXX",
      "mail": "",
      "tel": "",
      "fax": "701",
      "piva": "XXXXXX",
      "cf": "",
      "termini_pagamento": "0",
      "pagamento_fine_mese": false,
      "val_iva_default": "",
      "desc_iva_default": "",
      "extra": "",
      "PA": false
    }
  ],
  "pagina_corrente": 1,
  "numero_pagine": 1,
  "success": true
}

As you can see the JSON has an array lista_clienti and 3 values pagina_corrente , numero_pagine and success.

I can easely access the 3 values but i cannot understand how to access the array nested in lista_clienti.
Here is the code i wrote :

Dim i As Integer
Dim e As Xojo.Core.Dictionary
Dim d as Xojo.Core.Dictionary
// Display the results
Dim jsonData As Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)
d = Xojo.Data.ParseJSON(jsonData)
if d.value("success") then
      Msgbox "OK!"
      //HERE I SHOULD ENTER INSIDE THE ARRAY AND COPY THE CONTENT IN  A LISTBOX
else
     Msgbox "ERROR"
end if

I tried with an array declared as Auto but it remains always empty and i don’t know how to populate it.

Thanks
Mattia

dim arr() as auto = d.Value( "lista_clienti" )
for each subdict as Xojo.Core.Dictionary in arr
  //
  // Process each sub-dictionary
  //
next

Ciao Mattia,
Kem is right.
But if you want to be sure (maybe for some reason lista_clienti is not there)
I suggest to use something like this:

dim arr() as Auto arr=d.lookUp("lista_clienti", arr)

Good catch.

If you want to distinguish between those two cases, then something like this:

dim arr() as auto
try
  arr = d.Value( "lista_clienti" )
catch err as KeyNotFoundException
  // wasn't there, so act accordingly
end try

Thanks to both of you!

Just to clarify an aspect :
the API i use return success in case the query was successfull. So why “lista_clienti” shoudn’t be there if d.value(“success”) is true ?

Mattia

If that’s what the spec calls for, it should be. But it’s always good to be proactive when it comes to error handling.

Moreover, if you control the software that produce the json you will have no problem (but as Kem says it is better to be proactive); but if you only consume it, then if something changes (for example lista_clienti becomes listaClienti, or will not exists if empty) then your code will not crash.