Xbase++
Xbase++
Loading and Parsing a Complex JSON Array
See more JSON Examples
This example loads a JSON array containing more complex data. It shows how to parse (access) various values contained within the JSON.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oSb
LOCAL nBCrlf
LOCAL oJsonArray
LOCAL nNumRecords
LOCAL i
LOCAL oJsonRecord
LOCAL nNumTelefones
LOCAL j
LOCAL nNumContatos
LOCAL k
LOCAL nNumEmails
nSuccess := 0
// This is the JSON we'll be loading:
// [
// {
// "telefones": [
// {
// "numero": "19995555555",
// "tipo": "T",
// "id": 2541437
// }
// ],
// "cnpj": "11395551000164",
// "rua": "R XAVIER AUGUSTO ROGGE, 22",
// "complemento": "",
// "contatos": [
// ],
// "tipo": "J",
// "razao_social": "SOUP BRASIL LTDA - ME",
// "nome_fantasia": "SOUP BRASIL",
// "bairro": "ABC DOS COLIBRIS",
// "cidade": "TEST",
// "inscricao_estadual": "222.102.222.116",
// "observacao": "",
// "id": 2209595,
// "ultima_alteracao": "2016-12-26 16:22:34",
// "cep": "13555000",
// "suframa": "",
// "estado": "SP",
// "emails": [
// {
// "email": "somebody@terra.com.br",
// "tipo": "T",
// "id": 1065557
// }
// ],
// "excluido": false
// },
// {
// "telefones": [
// ],
// "cnpj": "12496555500180",
// "rua": "AV ROLF WIEST, 100",
// "complemento": "ANDAR 7 SALA 612 A 620",
// "contatos": [
// ],
// "tipo": "J",
// "razao_social": "SIMPLE SOFTWARE LTDA",
// "nome_fantasia": "",
// "bairro": "DOM ZETIRO",
// "cidade": "APARTVILLE",
// "inscricao_estadual": "",
// "observacao": "",
// "id": 2255594,
// "ultima_alteracao": "2016-12-26 16:28:31",
// "cep": "89255505",
// "suframa": "",
// "estado": "SC",
// "emails": [
// ],
// "excluido": false
// },
// {
// "telefones": [
// {
// "numero": "1938655556",
// "tipo": "T",
// "id": 2555438
// }
// ],
// "cnpj": "00003555500153",
// "rua": "AV ABCDEF PINTO CATAO, 18",
// "complemento": "",
// "contatos": [
// {
// "telefones": [
// {
// "numero": "1999655554",
// "tipo": "T",
// "id": 2555559
// }
// ],
// "cargo": "zzz de compras",
// "nome": "Gerard",
// "emails": [
// {
// "email": "gerard@terra.com.br",
// "tipo": "T",
// "id": 1065559
// }
// ],
// "id": 844485,
// "excluido": false
// }
// ],
// "tipo": "J",
// "razao_social": "TIDY TECNOLOGIA LTDA - EPP",
// "nome_fantasia": "TIDY",
// "bairro": "TUNA",
// "cidade": "JAGUAR",
// "inscricao_estadual": "395.222.441.222",
// "observacao": "ligar sempre depois das 14hs",
// "id": 2255597,
// "ultima_alteracao": "2016-12-28 07:31:52",
// "cep": "13555500",
// "suframa": "",
// "estado": "SP",
// "emails": [
// {
// "email": "xi@tidy.com.br",
// "tipo": "T",
// "id": 10655558
// }
// ],
// "excluido": false
// }
// ]
//
// Construct a StringBuilder containing the above JSON array.
oSb := CreateObject("Chilkat.StringBuilder")
nBCrlf := 1
oSb:AppendLine("[", nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "telefones": [', nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "numero": "19995555555",', nBCrlf)
oSb:AppendLine(' "tipo": "T",', nBCrlf)
oSb:AppendLine(' "id": 2541437', nBCrlf)
oSb:AppendLine(" }", nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "cnpj": "11395551000164",', nBCrlf)
oSb:AppendLine(' "rua": "R XAVIER AUGUSTO ROGGE, 22",', nBCrlf)
oSb:AppendLine(' "complemento": "",', nBCrlf)
oSb:AppendLine(' "contatos": [', nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "tipo": "J",', nBCrlf)
oSb:AppendLine(' "razao_social": "SOUP BRASIL LTDA - ME",', nBCrlf)
oSb:AppendLine(' "nome_fantasia": "SOUP BRASIL",', nBCrlf)
oSb:AppendLine(' "bairro": "ABC DOS COLIBRIS",', nBCrlf)
oSb:AppendLine(' "cidade": "TEST",', nBCrlf)
oSb:AppendLine(' "inscricao_estadual": "222.102.222.116",', nBCrlf)
oSb:AppendLine(' "observacao": "",', nBCrlf)
oSb:AppendLine(' "id": 2209595,', nBCrlf)
oSb:AppendLine(' "ultima_alteracao": "2016-12-26 16:22:34",', nBCrlf)
oSb:AppendLine(' "cep": "13555000",', nBCrlf)
oSb:AppendLine(' "suframa": "",', nBCrlf)
oSb:AppendLine(' "estado": "SP",', nBCrlf)
oSb:AppendLine(' "emails": [', nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "email": "somebody@terra.com.br",', nBCrlf)
oSb:AppendLine(' "tipo": "T",', nBCrlf)
oSb:AppendLine(' "id": 1065557', nBCrlf)
oSb:AppendLine(" }", nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "excluido": false', nBCrlf)
oSb:AppendLine(" },", nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "telefones": [', nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "cnpj": "12496555500180",', nBCrlf)
oSb:AppendLine(' "rua": "AV ROLF WIEST, 100",', nBCrlf)
oSb:AppendLine(' "complemento": "ANDAR 7 SALA 612 A 620",', nBCrlf)
oSb:AppendLine(' "contatos": [', nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "tipo": "J",', nBCrlf)
oSb:AppendLine(' "razao_social": "SIMPLE SOFTWARE LTDA",', nBCrlf)
oSb:AppendLine(' "nome_fantasia": "",', nBCrlf)
oSb:AppendLine(' "bairro": "DOM ZETIRO",', nBCrlf)
oSb:AppendLine(' "cidade": "APARTVILLE",', nBCrlf)
oSb:AppendLine(' "inscricao_estadual": "",', nBCrlf)
oSb:AppendLine(' "observacao": "",', nBCrlf)
oSb:AppendLine(' "id": 2255594,', nBCrlf)
oSb:AppendLine(' "ultima_alteracao": "2016-12-26 16:28:31",', nBCrlf)
oSb:AppendLine(' "cep": "89255505",', nBCrlf)
oSb:AppendLine(' "suframa": "",', nBCrlf)
oSb:AppendLine(' "estado": "SC",', nBCrlf)
oSb:AppendLine(' "emails": [', nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "excluido": false', nBCrlf)
oSb:AppendLine(" },", nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "telefones": [', nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "numero": "1938655556",', nBCrlf)
oSb:AppendLine(' "tipo": "T",', nBCrlf)
oSb:AppendLine(' "id": 2555438', nBCrlf)
oSb:AppendLine(" }", nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "cnpj": "00003555500153",', nBCrlf)
oSb:AppendLine(' "rua": "AV ABCDEF PINTO CATAO, 18",', nBCrlf)
oSb:AppendLine(' "complemento": "",', nBCrlf)
oSb:AppendLine(' "contatos": [', nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "telefones": [', nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "numero": "1999655554",', nBCrlf)
oSb:AppendLine(' "tipo": "T",', nBCrlf)
oSb:AppendLine(' "id": 2555559', nBCrlf)
oSb:AppendLine(" }", nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "cargo": "zzz de compras",', nBCrlf)
oSb:AppendLine(' "nome": "Gerard",', nBCrlf)
oSb:AppendLine(' "emails": [', nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "email": "gerard@terra.com.br",', nBCrlf)
oSb:AppendLine(' "tipo": "T",', nBCrlf)
oSb:AppendLine(' "id": 1065559', nBCrlf)
oSb:AppendLine(" }", nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "id": 844485,', nBCrlf)
oSb:AppendLine(' "excluido": false', nBCrlf)
oSb:AppendLine(" }", nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "tipo": "J",', nBCrlf)
oSb:AppendLine(' "razao_social": "TIDY TECNOLOGIA LTDA - EPP",', nBCrlf)
oSb:AppendLine(' "nome_fantasia": "TIDY",', nBCrlf)
oSb:AppendLine(' "bairro": "TUNA",', nBCrlf)
oSb:AppendLine(' "cidade": "JAGUAR",', nBCrlf)
oSb:AppendLine(' "inscricao_estadual": "395.222.441.222",', nBCrlf)
oSb:AppendLine(' "observacao": "ligar sempre depois das 14hs",', nBCrlf)
oSb:AppendLine(' "id": 2255597,', nBCrlf)
oSb:AppendLine(' "ultima_alteracao": "2016-12-28 07:31:52",', nBCrlf)
oSb:AppendLine(' "cep": "13555500",', nBCrlf)
oSb:AppendLine(' "suframa": "",', nBCrlf)
oSb:AppendLine(' "estado": "SP",', nBCrlf)
oSb:AppendLine(' "emails": [', nBCrlf)
oSb:AppendLine(" {", nBCrlf)
oSb:AppendLine(' "email": "xi@tidy.com.br",', nBCrlf)
oSb:AppendLine(' "tipo": "T",', nBCrlf)
oSb:AppendLine(' "id": 10655558', nBCrlf)
oSb:AppendLine(" }", nBCrlf)
oSb:AppendLine(" ],", nBCrlf)
oSb:AppendLine(' "excluido": false', nBCrlf)
oSb:AppendLine(" }", nBCrlf)
oSb:AppendLine("]", nBCrlf)
// Load the JSON array into a JsonArray:
oJsonArray := CreateObject("Chilkat.JsonArray")
nSuccess := oJsonArray:LoadSb(oSb)
IF (nSuccess != 1)
? oJsonArray:LastErrorText
oSb:destroy()
oJsonArray:destroy()
RETURN
ENDIF
// Get some information from each record in the array.
nNumRecords := oJsonArray:Size
i := 0
DO WHILE i < nNumRecords
? "------ Record " + Str(i) + " -------"
oJsonRecord := oJsonArray:ObjectAt(i)
// Examine information for this record
nNumTelefones := oJsonRecord:SizeOfArray("telefones")
? "Number of telefones: " + Str(nNumTelefones)
j := 0
DO WHILE j < nNumTelefones
oJsonRecord:J := j
? " telefones numero: " + oJsonRecord:StringOf("telefones[j].numero")
? " telefones tipo: " + oJsonRecord:StringOf("telefones[j].tipo")
? " telefones id: " + oJsonRecord:StringOf("telefones[j].id")
j := j + 1
ENDDO
? "cnpj: " + oJsonRecord:StringOf("cnpj")
? "rua: " + oJsonRecord:StringOf("rua")
// ...
nNumContatos := oJsonRecord:SizeOfArray("contatos")
? "Number of contatos: " + Str(nNumContatos)
j := 0
DO WHILE j < nNumContatos
oJsonRecord:J := j
nNumTelefones := oJsonRecord:SizeOfArray("contatos[j].telefones")
? " Number of telefones: " + Str(nNumTelefones)
k := 0
DO WHILE k < nNumTelefones
oJsonRecord:K := k
? " telefones numero: " + oJsonRecord:StringOf("contatos[j].telefones[k].numero")
? " telefones tipo: " + oJsonRecord:StringOf("contatos[j].telefones[k].tipo")
? " telefones id: " + oJsonRecord:StringOf("contatos[j].telefones[k].id")
k := k + 1
ENDDO
? " cargo: " + oJsonRecord:StringOf("contatos[j].cargo")
nNumEmails := oJsonRecord:SizeOfArray("contatos[j].emails")
? " Number of emails: " + Str(nNumEmails)
k := 0
DO WHILE k < nNumEmails
oJsonRecord:K := k
? " emails email: " + oJsonRecord:StringOf("contatos[j].emails[k].email")
? " emails tipo: " + oJsonRecord:StringOf("contatos[j].emails[k].tipo")
? " emails id: " + oJsonRecord:StringOf("contatos[j].emails[k].id")
k := k + 1
ENDDO
j := j + 1
ENDDO
oJsonRecord:destroy()
i := i + 1
ENDDO
// The output for the above code is:
// ------ Record 0 -------
// Number of telefones: 1
// telefones numero: 19995555555
// telefones tipo: T
// telefones id: 2541437
// cnpj: 11395551000164
// rua: R XAVIER AUGUSTO ROGGE, 22
// Number of contatos: 0
// ------ Record 1 -------
// Number of telefones: 0
// cnpj: 12496555500180
// rua: AV ROLF WIEST, 100
// Number of contatos: 0
// ------ Record 2 -------
// Number of telefones: 1
// telefones numero: 1938655556
// telefones tipo: T
// telefones id: 2555438
// cnpj: 00003555500153
// rua: AV ABCDEF PINTO CATAO, 18
// Number of contatos: 1
// Number of telefones: 1
// telefones numero: 1999655554
// telefones tipo: T
// telefones id: 2555559
// cargo: zzz de compras
// Number of emails: 1
// emails email: gerard@terra.com.br
// emails tipo: T
// emails id: 1065559
//
oSb:destroy()
oJsonArray:destroy()