Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Delphi JSON TO TSV
//
// RESTから来たJSONデータからタブ区切り形式に変換
//
procedure TForm1.RESTRequest1AfterExecute(Sender: TCustomRESTRequest);
var
ja: TJSONArray;
i1,i2: Integer;
row: TJSONObject; //レコード
date: TDateTime;
stRow: String;
stColumn: String;
begin
date := Now;
ja := RESTResponse1.JSONValue.GetValue<TJSONArray>(); //配列として取得
for i1 := 0 to ja.Count-1 do
begin
row := ja.Items[i1].GetValue<TJSONObject>; //配列を取る
stRow := '';
stColumn := '';
for i2:=0 to row.Count-1 do
begin
case i1 of
0: begin
stColumn := stColumn + row.Pairs[i2].JsonString.Value + #9; //JSONのフィールド名取得
end;
end;
stRow := stRow + row.Pairs[i2].JsonValue.Value + #9; //JSONのVALUE取得
end;
if i1 = 0 then
begin
Delete(stColumn, Length(stColumn), 1);
Memo1.Lines.Append(stColumn); //CSVの最初のColumn名
end;
Delete( stRow, Length(stRow), 1);
Memo1.Lines.Append(stRow); //行を追記する
end;
date := Now - date;
Memo1.Lines.Append(FormatDateTime('hh:nn:ss', date));
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment