Android Question Error Map Parser

joaomoraes83

Member
Licensed User
Hello everybody!
When running the code below,
B4X:
ProgressDialogShow2("Carregando Dados...",False)
    Dim GetAddressJob As HttpJob
    GetAddressJob.Initialize("GetAddress", Me)
    
    GetAddressJob.Download("https://api.portaldatransparencia.gov.br/api-de-dados/ceis?codigoSancionado=09743631895&pagina=1")
    GetAddressJob.GetRequest.SetHeader("chave-api-dados",API_KEY)
    
    Wait For (GetAddressJob) JobDone(GetAddressJob As HttpJob)
    ProgressDialogHide
    If GetAddressJob.Success Then
    
        Dim parser As JSONParser
        parser.Initialize(GetAddressJob.GetString)
        Dim root As List = parser.NextArray
        For Each colroot As Map In root
            Dim dataInicioSancao As String = colroot.Get("dataInicioSancao")
            Dim numeroProcesso As String = colroot.Get("numeroProcesso")
            Dim pessoa As Map = colroot.Get("pessoa")
            Dim cnae As Map = pessoa.Get("cnae")
            Dim grupo As String = cnae.Get("grupo")
            Dim razaoSocialReceita As String = pessoa.Get("razaoSocialReceita")
            Dim dataFimSancao As String = colroot.Get("dataFimSancao")
            Dim orgaoSancionador As Map = colroot.Get("orgaoSancionador")
            Dim nome As String = orgaoSancionador.Get("nome")
        Next

        Log($"razaoSocialReceita: ${razaoSocialReceita}"$)
        Log($"grupo: ${grupo}"$)
        Log($"dataInicioSancao: ${dataInicioSancao}"$)
        Log($"dataFimSancao: ${dataFimSancao}"$)
        '        Log($"orgaoSancionador: ${nome}"$)
        Log($"numeroProcesso: ${numeroProcesso}"$)
    
    End If

I get the following error:
B4X:
java.lang.RuntimeException: Object should first be initialized (Map).

where can i be wrong?
 

Sagenut

Expert
Licensed User
Longtime User
Can you post the JSON string obtained from the request?
 
Upvote 0

Rubsanpe

Active Member
Licensed User
Hi. Are you sure the code

B4X:
Dim pessoa As Map = colroot.Get("pessoa")
Dim cnae As Map = pessoa.Get("cnae")
Dim grupo As String = cnae.Get("grupo")
Dim razaoSocialReceita As String = pessoa.Get("razaoSocialReceita")
Dim dataFimSancao As String = colroot.Get("dataFimSancao")
Dim orgaoSancionador As Map = colroot.Get("orgaoSancionador")

return a Map in each data? I think "pessoa", "cnae" and "orgaoSancionador" are strings like the other data.

Rubén
 
Upvote 0

joaomoraes83

Member
Licensed User
my friend you are correct, I changed from map to string and it ran without problem.

thank you all
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…