Android Question Json key null

dibesw

Active Member
Licensed User
Longtime User
I have an error when I read Json file if a key is null.
if 37 key exist, it works
can someone help me? Thanks!!
this is my json file:

json1.jpg



json2.jpg



This in my code:

B4X:
Sub ParseEvents
    Dim parser As JSONParser
    parser.Initialize(IncomingJSONText)
    Dim root As List = parser.NextArray
    Dim conta As Int = root.Size
    listaP.Initialize
    listaP.Clear
    For Each colroot As Map In root
       nome = colroot.Get("nome")
       cftp = colroot.Get("Foto")
       codice = colroot.Get("id")
       elenco.AddSingleLine(nome)
       listaP.Add(codice)
    Next
End Sub
 

dibesw

Active Member
Licensed User
Longtime User
key 37 in null because it was cancelled. it is normal this?
How I can skip this null value?
I tried:
B4X:
If colroot.Get("nome")<>Null Then...
If callroot.size<>0<>Null Then...
If colroot.ContainsKey("nome") Then...

but I have always error in loop :(
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The problem is that with your for next loop you automatically assume that every list item is a Map, which is not true in your case.
Try:
B4X:
Sub ParseEvents
   Dim parser As JSONParser
   parser.Initialize(IncomingJSONText)
   Dim root As List = parser.NextArray
   Dim conta As Int = root.Size
   listaP.Initialize
   listaP.Clear
   Dim colitem As Map
   For Each colroot As Object In root
       If (colroot <> Null) And GetType(colroot).EndsWith("Map") Then
           colitem = colroot
           nome = colitem.Get("nome")
           cftp = colitem.Get("Foto")
           codice = colitem.Get("id")
           elenco.AddSingleLine(nome)
           listaP.Add(codice)
       End If
   Next
End Sub

Edit: removed some of my dim's to make code work
 
Upvote 0
Top