Java
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// 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.
StringBuilder sb = new StringBuilder();
boolean bCrlf = true;
sb.appendLine("[", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"telefones\": [", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"numero\": \"19995555555\",", bCrlf);
sb.appendLine(" \"tipo\": \"T\",", bCrlf);
sb.appendLine(" \"id\": 2541437", bCrlf);
sb.appendLine(" }", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"cnpj\": \"11395551000164\",", bCrlf);
sb.appendLine(" \"rua\": \"R XAVIER AUGUSTO ROGGE, 22\",", bCrlf);
sb.appendLine(" \"complemento\": \"\",", bCrlf);
sb.appendLine(" \"contatos\": [", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"tipo\": \"J\",", bCrlf);
sb.appendLine(" \"razao_social\": \"SOUP BRASIL LTDA - ME\",", bCrlf);
sb.appendLine(" \"nome_fantasia\": \"SOUP BRASIL\",", bCrlf);
sb.appendLine(" \"bairro\": \"ABC DOS COLIBRIS\",", bCrlf);
sb.appendLine(" \"cidade\": \"TEST\",", bCrlf);
sb.appendLine(" \"inscricao_estadual\": \"222.102.222.116\",", bCrlf);
sb.appendLine(" \"observacao\": \"\",", bCrlf);
sb.appendLine(" \"id\": 2209595,", bCrlf);
sb.appendLine(" \"ultima_alteracao\": \"2016-12-26 16:22:34\",", bCrlf);
sb.appendLine(" \"cep\": \"13555000\",", bCrlf);
sb.appendLine(" \"suframa\": \"\",", bCrlf);
sb.appendLine(" \"estado\": \"SP\",", bCrlf);
sb.appendLine(" \"emails\": [", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"email\": \"somebody@terra.com.br\",", bCrlf);
sb.appendLine(" \"tipo\": \"T\",", bCrlf);
sb.appendLine(" \"id\": 1065557", bCrlf);
sb.appendLine(" }", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"excluido\": false", bCrlf);
sb.appendLine(" },", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"telefones\": [", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"cnpj\": \"12496555500180\",", bCrlf);
sb.appendLine(" \"rua\": \"AV ROLF WIEST, 100\",", bCrlf);
sb.appendLine(" \"complemento\": \"ANDAR 7 SALA 612 A 620\",", bCrlf);
sb.appendLine(" \"contatos\": [", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"tipo\": \"J\",", bCrlf);
sb.appendLine(" \"razao_social\": \"SIMPLE SOFTWARE LTDA\",", bCrlf);
sb.appendLine(" \"nome_fantasia\": \"\",", bCrlf);
sb.appendLine(" \"bairro\": \"DOM ZETIRO\",", bCrlf);
sb.appendLine(" \"cidade\": \"APARTVILLE\",", bCrlf);
sb.appendLine(" \"inscricao_estadual\": \"\",", bCrlf);
sb.appendLine(" \"observacao\": \"\",", bCrlf);
sb.appendLine(" \"id\": 2255594,", bCrlf);
sb.appendLine(" \"ultima_alteracao\": \"2016-12-26 16:28:31\",", bCrlf);
sb.appendLine(" \"cep\": \"89255505\",", bCrlf);
sb.appendLine(" \"suframa\": \"\",", bCrlf);
sb.appendLine(" \"estado\": \"SC\",", bCrlf);
sb.appendLine(" \"emails\": [", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"excluido\": false", bCrlf);
sb.appendLine(" },", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"telefones\": [", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"numero\": \"1938655556\",", bCrlf);
sb.appendLine(" \"tipo\": \"T\",", bCrlf);
sb.appendLine(" \"id\": 2555438", bCrlf);
sb.appendLine(" }", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"cnpj\": \"00003555500153\",", bCrlf);
sb.appendLine(" \"rua\": \"AV ABCDEF PINTO CATAO, 18\",", bCrlf);
sb.appendLine(" \"complemento\": \"\",", bCrlf);
sb.appendLine(" \"contatos\": [", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"telefones\": [", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"numero\": \"1999655554\",", bCrlf);
sb.appendLine(" \"tipo\": \"T\",", bCrlf);
sb.appendLine(" \"id\": 2555559", bCrlf);
sb.appendLine(" }", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"cargo\": \"zzz de compras\",", bCrlf);
sb.appendLine(" \"nome\": \"Gerard\",", bCrlf);
sb.appendLine(" \"emails\": [", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"email\": \"gerard@terra.com.br\",", bCrlf);
sb.appendLine(" \"tipo\": \"T\",", bCrlf);
sb.appendLine(" \"id\": 1065559", bCrlf);
sb.appendLine(" }", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"id\": 844485,", bCrlf);
sb.appendLine(" \"excluido\": false", bCrlf);
sb.appendLine(" }", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"tipo\": \"J\",", bCrlf);
sb.appendLine(" \"razao_social\": \"TIDY TECNOLOGIA LTDA - EPP\",", bCrlf);
sb.appendLine(" \"nome_fantasia\": \"TIDY\",", bCrlf);
sb.appendLine(" \"bairro\": \"TUNA\",", bCrlf);
sb.appendLine(" \"cidade\": \"JAGUAR\",", bCrlf);
sb.appendLine(" \"inscricao_estadual\": \"395.222.441.222\",", bCrlf);
sb.appendLine(" \"observacao\": \"ligar sempre depois das 14hs\",", bCrlf);
sb.appendLine(" \"id\": 2255597,", bCrlf);
sb.appendLine(" \"ultima_alteracao\": \"2016-12-28 07:31:52\",", bCrlf);
sb.appendLine(" \"cep\": \"13555500\",", bCrlf);
sb.appendLine(" \"suframa\": \"\",", bCrlf);
sb.appendLine(" \"estado\": \"SP\",", bCrlf);
sb.appendLine(" \"emails\": [", bCrlf);
sb.appendLine(" {", bCrlf);
sb.appendLine(" \"email\": \"xi@tidy.com.br\",", bCrlf);
sb.appendLine(" \"tipo\": \"T\",", bCrlf);
sb.appendLine(" \"id\": 10655558", bCrlf);
sb.appendLine(" }", bCrlf);
sb.appendLine(" ],", bCrlf);
sb.appendLine(" \"excluido\": false", bCrlf);
sb.appendLine(" }", bCrlf);
sb.appendLine("]", bCrlf);
// Load the JSON array into a JsonArray:
JsonArray jsonArray = new JsonArray();
try {
jsonArray.loadSb(sb);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Get some information from each record in the array.
int numRecords = jsonArray.getSize();
int i = 0;
while (i < numRecords) {
System.out.println("------ Record " + i + " -------");
JsonObject jsonRecord = jsonArray.objectAt(i);
// Examine information for this record
int numTelefones = jsonRecord.sizeOfArray("telefones");
System.out.println("Number of telefones: " + numTelefones);
int j = 0;
while (j < numTelefones) {
jsonRecord.setJ(j);
System.out.println(" telefones numero: " + jsonRecord.stringOf("telefones[j].numero"));
System.out.println(" telefones tipo: " + jsonRecord.stringOf("telefones[j].tipo"));
System.out.println(" telefones id: " + jsonRecord.stringOf("telefones[j].id"));
j = j + 1;
}
System.out.println("cnpj: " + jsonRecord.stringOf("cnpj"));
System.out.println("rua: " + jsonRecord.stringOf("rua"));
// ...
int numContatos = jsonRecord.sizeOfArray("contatos");
System.out.println("Number of contatos: " + numContatos);
j = 0;
while (j < numContatos) {
jsonRecord.setJ(j);
numTelefones = jsonRecord.sizeOfArray("contatos[j].telefones");
System.out.println(" Number of telefones: " + numTelefones);
int k = 0;
while (k < numTelefones) {
jsonRecord.setK(k);
System.out.println(" telefones numero: " + jsonRecord.stringOf("contatos[j].telefones[k].numero"));
System.out.println(" telefones tipo: " + jsonRecord.stringOf("contatos[j].telefones[k].tipo"));
System.out.println(" telefones id: " + jsonRecord.stringOf("contatos[j].telefones[k].id"));
k = k + 1;
}
System.out.println(" cargo: " + jsonRecord.stringOf("contatos[j].cargo"));
int numEmails = jsonRecord.sizeOfArray("contatos[j].emails");
System.out.println(" Number of emails: " + numEmails);
k = 0;
while (k < numEmails) {
jsonRecord.setK(k);
System.out.println(" emails email: " + jsonRecord.stringOf("contatos[j].emails[k].email"));
System.out.println(" emails tipo: " + jsonRecord.stringOf("contatos[j].emails[k].tipo"));
System.out.println(" emails id: " + jsonRecord.stringOf("contatos[j].emails[k].id"));
k = k + 1;
}
j = j + 1;
}
jsonRecord.close();
i = i + 1;
}
// 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
//
}
}