I want to produce something like this:
{
\"nrVendas\": 2,
\"totalVendas\": 100.0,
\"vendas\": [
{
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);