2
I need to generate a JSON file with Delphi 7. I use the LKJSON library.
I even managed to generate the file, but it was not the way it would need.
Below code I used to generate JSON:
procedure TForm1.Button3Click(Sender: TObject);
var
js: TlkJSONobject;
s: string;
i : Integer;
begin
SQLConnection1.Open;
taUF.Open;
js := TlkJSONobject.Create;
try
js.Add('estado', TlkJSONobject.Create);
taUF.First;
while not taUF.Eof do begin
with TlkJSONobject(TlkJSONobject(js['estado'])) do begin
Add('sigla', taUF.FieldByName('UFSigla').AsString);
Add('nome', taUF.FieldByName('UFNome').AsString);
end;
taUF.Next;
end;//while
i := 0;
s := GenerateReadableText(js, i);
Clipboard.SetTextBuf(PChar(s));
finally
js.Free;
end;
end;
This is the result obtained:
{
"estado":{
"sigla":"RJ",
"nome":"RIO DE JANEIRO",
"sigla":"SC",
"nome":"SANTA CATARINA"
}
}
I need it to stay this way:
{
"estado":
{
"sigla":"RJ",
"nome":"RIO DE JANEIRO"
},
{"sigla":"SC",
"nome":"SANTA CATARINA"
}
}
The structure of JSON that you want to return, is it valid? test here: http://jsonlint.com/
– stderr
This shape you want to generate is incorrect. I haven’t analyzed your code yet, but by the structure you’re trying to build,I believe you need to work with Lists.
– Victor Tadashi
Tip: Inside
with TlkJSONobject
you should add a new object likejs.Add('sigla_estado_aqui', TlkJSONobject.Create);
. After that, add the acronym and name to this created object in the loop.– Marcelo de Andrade
As @Victortadashi said, the Json structure you want to use is incorrect. There is no way a key ("state") has more than one value unless this value is a json array.
– Clayton A. Alves