Lima Kewnantchy
Lima Kewnantchy

Reputation: 13

Generate JSON array with LKJSON in Delphi 7

I want to produce something like this:

{
  "nrVendas": 2, 
  "totalVendas": 100.0, 
  "vendas": [ 
    {
      "nsuOrigem": "1",
      "data": "2014-03-14",
      "nrParcelas": 1,
      "valor": 50,
      "parcelas": [
        {
          "numero": 1,
          "valor": 50
        }
      ]
    }, 
    {
      "nsuOrigem": "2",
      "data": "2014-03-14",
      "nrParcelas": 1,
      "valor": 50,
      "parcelas": [
        {
          "numero": 1,
          "valor": 50
        }
      ]
    }
  ]
}

I'm trying it this way:

js3 := TlkJSONobject.Create;
js3.Add('numero', '1');
js3.Add('valor', '50');

js32 :=  TlkJSONobject.Create;
js32.Add('numero', '1');
js32.Add('valor', '100');

js2 := TlkJSONobject.Create;
js2.Add('nsuOrigem', '1');
js2.Add('data', '2014-02-02');
js2.Add('nrParcelas', 1);
js2.Add('valor', 50);
js2.Add('parcelas', js3);

js2.Add('nsuOrigem', '2');
js2.Add('data', '2014-02-02');
js2.Add('nrParcelas', 1);
js2.Add('valor', 50);
js2.Add('parcelas', js32);

js0 := TlkJSONobject.Create;
js0.Add('numeroVendas', 2);
js0.Add('totalVendas', 100.0);
js0.Add('vendas', js2);

i := 0;
s := GenerateReadableText(js0, i);

Memo2.Lines.Add(s);

But I'm receiving this instead:

{
  "numeroVendas": 2,
  "totalVendas": 100,
  "vendas": {
    "nsuOrigem": "1",
    "data": "2014-02-02",
    "nrParcelas": 1,
    "valor": 50,
    "parcelas": {
      "numero": "1",
      "valor": "50"
    },
    "nsuOrigem": "2",
    "data": "2014-02-02",
    "nrParcelas": 1,
    "valor": 50,
    "parcelas": {
      "numero": "1",
      "valor": "100"
    }
  }
}

Then I try to validade this JSON, but i received this error:

SyntaxError: Duplicate key 'nsuOrigem' on line 13

I think I need to use an array on the fields inside vendas, but I searched in all of the documentation and more, but I can't find anything that can help me.

Upvotes: 0

Views: 7858

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595320

You are not creating any arrays at all. vendas and parcelas are both an array of objects, but you are creating them as through they are single objects instead.

You are also creating a few of the JSON values as strings instead of as numbers.

Use the TlkJSONlist class to create arrays, eg:

js4_1 := TlkJSONobject.Create;
js4_1.Add('numero', 1);
js4_1.Add('valor', 50);

js4_2 := TlkJSONobject.Create;
js4_2.Add('numero', 1);
js4_2.Add('valor', 100);

js3_1 := TlkSONlist.Create;
js3_1.Add(js4_1);

js3_2 := TlkSONlist.Create;
js3_2.Add(js4_2);

js2_1 := TlkJSONobject.Create;
js2_1.Add('nsuOrigem', '1');
js2_1.Add('data', '2014-02-02');
js2_1.Add('nrParcelas', 1);
js2_1.Add('valor', 50);
js2_1.Add('parcelas', js3_1);

js2_2.Add('nsuOrigem', '2');
js2_2.Add('data', '2014-02-02');
js2_2.Add('nrParcelas', 1);
js2_2.Add('valor', 50);
js2_2.Add('parcelas', js3_2);

js1 := TlkJSONlist.Create;
js1.Add(js2_1);
js1.Add(js2_2);

js0 := TlkJSONobject.Create;
js0.Add('numeroVendas', 2);
js0.Add('totalVendas', 100.0);
js0.Add('vendas', js1);

i := 0;
s := GenerateReadableText(js0, i);

Memo2.Lines.Add(s);

But please, give your variables more meaningful names! And change the order of your class constructions, it will make the code much easier to read and follow, eg:

root := TlkJSONobject.Create;
root.Add('numeroVendas', 2);
root.Add('totalVendas', 100.0);
vendas := TlkJSONList.Create;
root.Add('vendas', vendas);

venda := TlkJSONobject.Create;
vendas.Add(venda);
venda.Add('nsuOrigem', '1');
venda.Add('data', '2014-02-02');
venda.Add('nrParcelas', 1);
venda.Add('valor', 50);
parcelas := TlkJSONlist.Create;
venda.Add('parcelas', parcelas);

parcela := TlkJSONobject.Create;
parcelas.Add(parcela);
parcela.Add('numero', 1);
parcela.Add('valor', 50);

venda := TlkJSONobject.Create;
vendas.Add(venda);
venda.Add('nsuOrigem', '1');
venda.Add('data', '2014-02-02');
venda.Add('nrParcelas', 1);
venda.Add('valor', 50);
parcelas := TlkJSONlist.Create;
venda.Add('parcelas', parcelas);

parcela := TlkJSONobject.Create;
parcelas.Add(parcela);
parcela.Add('numero', 1);
parcela.Add('valor', 100);

i := 0;
s := GenerateReadableText(root, i);

Memo2.Lines.Add(s);

Upvotes: 4

Related Questions