Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
// 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.
loSb = createobject("CkStringBuilder")
llBCrlf = .T.
loSb.AppendLine("[",llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "telefones": [',llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "numero": "19995555555",',llBCrlf)
loSb.AppendLine(' "tipo": "T",',llBCrlf)
loSb.AppendLine(' "id": 2541437',llBCrlf)
loSb.AppendLine(" }",llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "cnpj": "11395551000164",',llBCrlf)
loSb.AppendLine(' "rua": "R XAVIER AUGUSTO ROGGE, 22",',llBCrlf)
loSb.AppendLine(' "complemento": "",',llBCrlf)
loSb.AppendLine(' "contatos": [',llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "tipo": "J",',llBCrlf)
loSb.AppendLine(' "razao_social": "SOUP BRASIL LTDA - ME",',llBCrlf)
loSb.AppendLine(' "nome_fantasia": "SOUP BRASIL",',llBCrlf)
loSb.AppendLine(' "bairro": "ABC DOS COLIBRIS",',llBCrlf)
loSb.AppendLine(' "cidade": "TEST",',llBCrlf)
loSb.AppendLine(' "inscricao_estadual": "222.102.222.116",',llBCrlf)
loSb.AppendLine(' "observacao": "",',llBCrlf)
loSb.AppendLine(' "id": 2209595,',llBCrlf)
loSb.AppendLine(' "ultima_alteracao": "2016-12-26 16:22:34",',llBCrlf)
loSb.AppendLine(' "cep": "13555000",',llBCrlf)
loSb.AppendLine(' "suframa": "",',llBCrlf)
loSb.AppendLine(' "estado": "SP",',llBCrlf)
loSb.AppendLine(' "emails": [',llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "email": "somebody@terra.com.br",',llBCrlf)
loSb.AppendLine(' "tipo": "T",',llBCrlf)
loSb.AppendLine(' "id": 1065557',llBCrlf)
loSb.AppendLine(" }",llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "excluido": false',llBCrlf)
loSb.AppendLine(" },",llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "telefones": [',llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "cnpj": "12496555500180",',llBCrlf)
loSb.AppendLine(' "rua": "AV ROLF WIEST, 100",',llBCrlf)
loSb.AppendLine(' "complemento": "ANDAR 7 SALA 612 A 620",',llBCrlf)
loSb.AppendLine(' "contatos": [',llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "tipo": "J",',llBCrlf)
loSb.AppendLine(' "razao_social": "SIMPLE SOFTWARE LTDA",',llBCrlf)
loSb.AppendLine(' "nome_fantasia": "",',llBCrlf)
loSb.AppendLine(' "bairro": "DOM ZETIRO",',llBCrlf)
loSb.AppendLine(' "cidade": "APARTVILLE",',llBCrlf)
loSb.AppendLine(' "inscricao_estadual": "",',llBCrlf)
loSb.AppendLine(' "observacao": "",',llBCrlf)
loSb.AppendLine(' "id": 2255594,',llBCrlf)
loSb.AppendLine(' "ultima_alteracao": "2016-12-26 16:28:31",',llBCrlf)
loSb.AppendLine(' "cep": "89255505",',llBCrlf)
loSb.AppendLine(' "suframa": "",',llBCrlf)
loSb.AppendLine(' "estado": "SC",',llBCrlf)
loSb.AppendLine(' "emails": [',llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "excluido": false',llBCrlf)
loSb.AppendLine(" },",llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "telefones": [',llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "numero": "1938655556",',llBCrlf)
loSb.AppendLine(' "tipo": "T",',llBCrlf)
loSb.AppendLine(' "id": 2555438',llBCrlf)
loSb.AppendLine(" }",llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "cnpj": "00003555500153",',llBCrlf)
loSb.AppendLine(' "rua": "AV ABCDEF PINTO CATAO, 18",',llBCrlf)
loSb.AppendLine(' "complemento": "",',llBCrlf)
loSb.AppendLine(' "contatos": [',llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "telefones": [',llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "numero": "1999655554",',llBCrlf)
loSb.AppendLine(' "tipo": "T",',llBCrlf)
loSb.AppendLine(' "id": 2555559',llBCrlf)
loSb.AppendLine(" }",llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "cargo": "zzz de compras",',llBCrlf)
loSb.AppendLine(' "nome": "Gerard",',llBCrlf)
loSb.AppendLine(' "emails": [',llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "email": "gerard@terra.com.br",',llBCrlf)
loSb.AppendLine(' "tipo": "T",',llBCrlf)
loSb.AppendLine(' "id": 1065559',llBCrlf)
loSb.AppendLine(" }",llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "id": 844485,',llBCrlf)
loSb.AppendLine(' "excluido": false',llBCrlf)
loSb.AppendLine(" }",llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "tipo": "J",',llBCrlf)
loSb.AppendLine(' "razao_social": "TIDY TECNOLOGIA LTDA - EPP",',llBCrlf)
loSb.AppendLine(' "nome_fantasia": "TIDY",',llBCrlf)
loSb.AppendLine(' "bairro": "TUNA",',llBCrlf)
loSb.AppendLine(' "cidade": "JAGUAR",',llBCrlf)
loSb.AppendLine(' "inscricao_estadual": "395.222.441.222",',llBCrlf)
loSb.AppendLine(' "observacao": "ligar sempre depois das 14hs",',llBCrlf)
loSb.AppendLine(' "id": 2255597,',llBCrlf)
loSb.AppendLine(' "ultima_alteracao": "2016-12-28 07:31:52",',llBCrlf)
loSb.AppendLine(' "cep": "13555500",',llBCrlf)
loSb.AppendLine(' "suframa": "",',llBCrlf)
loSb.AppendLine(' "estado": "SP",',llBCrlf)
loSb.AppendLine(' "emails": [',llBCrlf)
loSb.AppendLine(" {",llBCrlf)
loSb.AppendLine(' "email": "xi@tidy.com.br",',llBCrlf)
loSb.AppendLine(' "tipo": "T",',llBCrlf)
loSb.AppendLine(' "id": 10655558',llBCrlf)
loSb.AppendLine(" }",llBCrlf)
loSb.AppendLine(" ],",llBCrlf)
loSb.AppendLine(' "excluido": false',llBCrlf)
loSb.AppendLine(" }",llBCrlf)
loSb.AppendLine("]",llBCrlf)
// Load the JSON array into a JsonArray:
loJsonArray = createobject("CkJsonArray")
llSuccess = loJsonArray.LoadSb(loSb)
if (llSuccess <> .T.) then
? loJsonArray.LastErrorText
release loSb
release loJsonArray
return
endif
// Get some information from each record in the array.
lnNumRecords = loJsonArray.Size
i = 0
do while i < lnNumRecords
? "------ Record " + str(i) + " -------"
loJsonRecord = loJsonArray.ObjectAt(i)
// Examine information for this record
lnNumTelefones = loJsonRecord.SizeOfArray("telefones")
? "Number of telefones: " + str(lnNumTelefones)
j = 0
do while j < lnNumTelefones
loJsonRecord.J = j
? " telefones numero: " + loJsonRecord.StringOf("telefones[j].numero")
? " telefones tipo: " + loJsonRecord.StringOf("telefones[j].tipo")
? " telefones id: " + loJsonRecord.StringOf("telefones[j].id")
j = j + 1
enddo
? "cnpj: " + loJsonRecord.StringOf("cnpj")
? "rua: " + loJsonRecord.StringOf("rua")
// ...
lnNumContatos = loJsonRecord.SizeOfArray("contatos")
? "Number of contatos: " + str(lnNumContatos)
j = 0
do while j < lnNumContatos
loJsonRecord.J = j
lnNumTelefones = loJsonRecord.SizeOfArray("contatos[j].telefones")
? " Number of telefones: " + str(lnNumTelefones)
k = 0
do while k < lnNumTelefones
loJsonRecord.K = k
? " telefones numero: " + loJsonRecord.StringOf("contatos[j].telefones[k].numero")
? " telefones tipo: " + loJsonRecord.StringOf("contatos[j].telefones[k].tipo")
? " telefones id: " + loJsonRecord.StringOf("contatos[j].telefones[k].id")
k = k + 1
enddo
? " cargo: " + loJsonRecord.StringOf("contatos[j].cargo")
lnNumEmails = loJsonRecord.SizeOfArray("contatos[j].emails")
? " Number of emails: " + str(lnNumEmails)
k = 0
do while k < lnNumEmails
loJsonRecord.K = k
? " emails email: " + loJsonRecord.StringOf("contatos[j].emails[k].email")
? " emails tipo: " + loJsonRecord.StringOf("contatos[j].emails[k].tipo")
? " emails id: " + loJsonRecord.StringOf("contatos[j].emails[k].id")
k = k + 1
enddo
j = j + 1
enddo
release loJsonRecord
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
//
release loSb
release loJsonArray