Generate JSON Delphi 7 file

Asked

Viewed 4,309 times

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/

  • 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.

  • Tip: Inside with TlkJSONobject you should add a new object like js.Add('sigla_estado_aqui', TlkJSONobject.Create);. After that, add the acronym and name to this created object in the loop.

  • 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.

1 answer

-1


As mentioned in the comments, Voce will have to work with lists to generate JSON, follow a code as an example:

procedure TForm2.Button1Click(Sender: TObject);
var
  JsonFull: TlkJSONobject;
  PropertyJson: TlkJSONobject;
  JsonList: TlkJSONlist;
  s: string;
  i: Integer;

begin
  JsonFull := TlkJSONobject.Create;
  JsonList := TlkJSONlist.Create;

  try
    for i := 1 to 5 do
    begin
      PropertyJson := TlkJSONobject.Create;
      PropertyJson.Add('sigla', 'UF' + IntToStr(i));
      PropertyJson.Add('nome', 'Estado' + IntToStr(i));
      JsonList.Add(PropertyJson);

    end;

    JsonFull.Add('estado', JsonList);

    i := 0;
    s := GenerateReadableText(JsonFull, i);
    Clipboard.SetTextBuf(PChar(s));

  finally
    FreeAndNil(JsonFull);
  end;

end;

3 new variables were created:

Jsonfull: Tlkjsonobject - That represents the JSON with the list of objects

Propertyjson: Tlkjsonobject - Representing each State

Jsonlist: Tlkjsonlist - Representing the list of states

With this each object will belong to the other, like a cascade:

PropertyJson.Add(Estado) > JsonList.Add(PropertyJson) > JsonFull.Add(JsonList)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.