Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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.
local sb = chilkat.StringBuilder.new()
local bCrlf = true
sb:append_line("[", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"telefones\": [", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"numero\": \"19995555555\",", bCrlf)
sb:append_line(" \"tipo\": \"T\",", bCrlf)
sb:append_line(" \"id\": 2541437", bCrlf)
sb:append_line(" }", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"cnpj\": \"11395551000164\",", bCrlf)
sb:append_line(" \"rua\": \"R XAVIER AUGUSTO ROGGE, 22\",", bCrlf)
sb:append_line(" \"complemento\": \"\",", bCrlf)
sb:append_line(" \"contatos\": [", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"tipo\": \"J\",", bCrlf)
sb:append_line(" \"razao_social\": \"SOUP BRASIL LTDA - ME\",", bCrlf)
sb:append_line(" \"nome_fantasia\": \"SOUP BRASIL\",", bCrlf)
sb:append_line(" \"bairro\": \"ABC DOS COLIBRIS\",", bCrlf)
sb:append_line(" \"cidade\": \"TEST\",", bCrlf)
sb:append_line(" \"inscricao_estadual\": \"222.102.222.116\",", bCrlf)
sb:append_line(" \"observacao\": \"\",", bCrlf)
sb:append_line(" \"id\": 2209595,", bCrlf)
sb:append_line(" \"ultima_alteracao\": \"2016-12-26 16:22:34\",", bCrlf)
sb:append_line(" \"cep\": \"13555000\",", bCrlf)
sb:append_line(" \"suframa\": \"\",", bCrlf)
sb:append_line(" \"estado\": \"SP\",", bCrlf)
sb:append_line(" \"emails\": [", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"email\": \"somebody@terra.com.br\",", bCrlf)
sb:append_line(" \"tipo\": \"T\",", bCrlf)
sb:append_line(" \"id\": 1065557", bCrlf)
sb:append_line(" }", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"excluido\": false", bCrlf)
sb:append_line(" },", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"telefones\": [", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"cnpj\": \"12496555500180\",", bCrlf)
sb:append_line(" \"rua\": \"AV ROLF WIEST, 100\",", bCrlf)
sb:append_line(" \"complemento\": \"ANDAR 7 SALA 612 A 620\",", bCrlf)
sb:append_line(" \"contatos\": [", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"tipo\": \"J\",", bCrlf)
sb:append_line(" \"razao_social\": \"SIMPLE SOFTWARE LTDA\",", bCrlf)
sb:append_line(" \"nome_fantasia\": \"\",", bCrlf)
sb:append_line(" \"bairro\": \"DOM ZETIRO\",", bCrlf)
sb:append_line(" \"cidade\": \"APARTVILLE\",", bCrlf)
sb:append_line(" \"inscricao_estadual\": \"\",", bCrlf)
sb:append_line(" \"observacao\": \"\",", bCrlf)
sb:append_line(" \"id\": 2255594,", bCrlf)
sb:append_line(" \"ultima_alteracao\": \"2016-12-26 16:28:31\",", bCrlf)
sb:append_line(" \"cep\": \"89255505\",", bCrlf)
sb:append_line(" \"suframa\": \"\",", bCrlf)
sb:append_line(" \"estado\": \"SC\",", bCrlf)
sb:append_line(" \"emails\": [", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"excluido\": false", bCrlf)
sb:append_line(" },", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"telefones\": [", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"numero\": \"1938655556\",", bCrlf)
sb:append_line(" \"tipo\": \"T\",", bCrlf)
sb:append_line(" \"id\": 2555438", bCrlf)
sb:append_line(" }", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"cnpj\": \"00003555500153\",", bCrlf)
sb:append_line(" \"rua\": \"AV ABCDEF PINTO CATAO, 18\",", bCrlf)
sb:append_line(" \"complemento\": \"\",", bCrlf)
sb:append_line(" \"contatos\": [", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"telefones\": [", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"numero\": \"1999655554\",", bCrlf)
sb:append_line(" \"tipo\": \"T\",", bCrlf)
sb:append_line(" \"id\": 2555559", bCrlf)
sb:append_line(" }", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"cargo\": \"zzz de compras\",", bCrlf)
sb:append_line(" \"nome\": \"Gerard\",", bCrlf)
sb:append_line(" \"emails\": [", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"email\": \"gerard@terra.com.br\",", bCrlf)
sb:append_line(" \"tipo\": \"T\",", bCrlf)
sb:append_line(" \"id\": 1065559", bCrlf)
sb:append_line(" }", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"id\": 844485,", bCrlf)
sb:append_line(" \"excluido\": false", bCrlf)
sb:append_line(" }", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"tipo\": \"J\",", bCrlf)
sb:append_line(" \"razao_social\": \"TIDY TECNOLOGIA LTDA - EPP\",", bCrlf)
sb:append_line(" \"nome_fantasia\": \"TIDY\",", bCrlf)
sb:append_line(" \"bairro\": \"TUNA\",", bCrlf)
sb:append_line(" \"cidade\": \"JAGUAR\",", bCrlf)
sb:append_line(" \"inscricao_estadual\": \"395.222.441.222\",", bCrlf)
sb:append_line(" \"observacao\": \"ligar sempre depois das 14hs\",", bCrlf)
sb:append_line(" \"id\": 2255597,", bCrlf)
sb:append_line(" \"ultima_alteracao\": \"2016-12-28 07:31:52\",", bCrlf)
sb:append_line(" \"cep\": \"13555500\",", bCrlf)
sb:append_line(" \"suframa\": \"\",", bCrlf)
sb:append_line(" \"estado\": \"SP\",", bCrlf)
sb:append_line(" \"emails\": [", bCrlf)
sb:append_line(" {", bCrlf)
sb:append_line(" \"email\": \"xi@tidy.com.br\",", bCrlf)
sb:append_line(" \"tipo\": \"T\",", bCrlf)
sb:append_line(" \"id\": 10655558", bCrlf)
sb:append_line(" }", bCrlf)
sb:append_line(" ],", bCrlf)
sb:append_line(" \"excluido\": false", bCrlf)
sb:append_line(" }", bCrlf)
sb:append_line("]", bCrlf)
-- Load the JSON array into a JsonArray:
local jsonArray = chilkat.JsonArray.new()
local success = jsonArray:load_sb(sb)
if not success then
print(jsonArray.last_error_text)
return
end
-- Get some information from each record in the array.
local numRecords = jsonArray.size
local i = 0
while i < numRecords do
print("------ Record " .. i .. " -------")
local jsonRecord = jsonArray:object_at(i)
-- Examine information for this record
local numTelefones = jsonRecord:size_of_array("telefones")
print("Number of telefones: " .. numTelefones)
local j = 0
while j < numTelefones do
jsonRecord.j = j
print(" telefones numero: " .. jsonRecord:string_of("telefones[j].numero"))
print(" telefones tipo: " .. jsonRecord:string_of("telefones[j].tipo"))
print(" telefones id: " .. jsonRecord:string_of("telefones[j].id"))
j = j + 1
end
print("cnpj: " .. jsonRecord:string_of("cnpj"))
print("rua: " .. jsonRecord:string_of("rua"))
-- ...
local numContatos = jsonRecord:size_of_array("contatos")
print("Number of contatos: " .. numContatos)
j = 0
while j < numContatos do
jsonRecord.j = j
numTelefones = jsonRecord:size_of_array("contatos[j].telefones")
print(" Number of telefones: " .. numTelefones)
local k = 0
while k < numTelefones do
jsonRecord.k = k
print(" telefones numero: " .. jsonRecord:string_of("contatos[j].telefones[k].numero"))
print(" telefones tipo: " .. jsonRecord:string_of("contatos[j].telefones[k].tipo"))
print(" telefones id: " .. jsonRecord:string_of("contatos[j].telefones[k].id"))
k = k + 1
end
print(" cargo: " .. jsonRecord:string_of("contatos[j].cargo"))
local numEmails = jsonRecord:size_of_array("contatos[j].emails")
print(" Number of emails: " .. numEmails)
k = 0
while k < numEmails do
jsonRecord.k = k
print(" emails email: " .. jsonRecord:string_of("contatos[j].emails[k].email"))
print(" emails tipo: " .. jsonRecord:string_of("contatos[j].emails[k].tipo"))
print(" emails id: " .. jsonRecord:string_of("contatos[j].emails[k].id"))
k = k + 1
end
j = j + 1
end
i = i + 1
end
-- 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
--