JUST SHARE - 使用 delphi SuperObject 将 MySQL 数据集转换为 Json 格式

发布于 2024-11-06 20:25:02 字数 3433 浏览 0 评论 0原文

只是分享我使用 Json using Delphi SuperObject 的经验

这个想法是如何将我的查询结果转换为 json轻松格式化。

我使用字符串来表示所有数据格式(包括BLOB、DATE、DATETIME)。将来可以轻松地将json文件导入到数据库中。

我不是从头开始编写代码,我只是根据自己的需要做了一些修改,

这是代码:

function TTableJSon.CreateJsonValueByFieldMySql(Json: ISuperObject;
  Field: TField): Boolean;
var
  JsonTyp, FieldTyp : string;
  tmpStr : string;
begin
  Result := False;
  if Field Is TDateField then begin
      Json.O[Field.FieldName] := SO('"'+FormatDateTime('yyyy-mm-dd',Field.AsDateTime)+'"')
  end else if Field Is TDateTimeField then begin
      Json.O[Field.FieldName] := SO('"'+FormatDateTime('yyyy-mm-dd hh:nn:ss',Field.AsDateTime)+'"')
  end else if Field is TMemoField then begin
      Json.S[Field.FieldName] := EncodeString(Field.AsString)
  end else if Field is TBlobField then begin
      Json.S[Field.FieldName] := EncodeString(Field.AsString)
  end else if Field is TFloatField then begin
      Json.O[Field.FieldName] := SO(ReplaceStr(Field.AsString,',','.'))
  end else begin
        Json.O[Field.FieldName] := SO(Field.Value);
  end;
  Result := True;
end;


function TTableJSon.JSonFromDataSet(DataSet: TDataSet): string;
  procedure GetFieldTypeInfo(Field:TField;var Fieldtyp,JsonTyp:string);
  begin
      Fieldtyp := GetEnumName(TypeInfo(TFieldType),ord(Field.DataType));
      Delete(Fieldtyp,1,2);
      if Field is TStringField then
        JsonTyp := 'string'
      else if Field is TDateTimeField then
        JsonTyp := 'integer'
      else if Field is TMemoField then
        JsonTyp := 'memo'
      else if Field is TBlobField then
        JsonTyp := 'blob'
      else if (Field is TIntegerField) or (Field is TLargeintField) then
        JsonTyp := 'integer'
      else if Field is TCurrencyField then
        JsonTyp := 'currency'
      else if Field is TNumericField then
        JsonTyp := 'double'
      else if Field is TBooleanField then
        JsonTyp := 'boolean'
      else
        JsonTyp := 'variant';
  end;

var
  sj,aj,sj2:ISuperObject;
  i:Integer;
  Fieldtyp,JsonTyp:string;
  List:TStringList;
begin
  sj := SO();
  aj := SA([]);
  List := TStringList.Create;
  try
      List.Sorted := True;

      for i := 0 to DataSet.FieldCount - 1 do
      begin
        sj2 := SO();
        GetFieldTypeInfo(DataSet.Fields[i],Fieldtyp,JsonTyp);

        sj2.S[cstFieldName] := DataSet.Fields[i].FieldName;
        sj2.S[cstFieldType] := Fieldtyp;
        sj2.S[cstJsonType] := JsonTyp;
        sj2.I[cstFieldSize] := DataSet.Fields[i].Size;
        sj2.B[cstRequired] := DataSet.Fields[i].Required;
        sj2.I[cstFieldIndex] := DataSet.Fields[i].Index;
        aj.AsArray.Add(sj2);
        List.Add(DataSet.Fields[i].FieldName+'='+JsonTyp);
      end;
      sj.O['Cols'] := aj;
      DataSet.DisableControls;

      DataSet.First;
      aj := SA([]);
      while not DataSet.Eof do
      begin
        sj2 := SO();
        for i := 0 to DataSet.FieldCount - 1 do
        begin
          if VarIsNull(DataSet.Fields[i].Value) then
            sj2.O[DataSet.Fields[i].FieldName] := SO(Null)
          else begin
            CreateJsonValueByFieldMySql(sj2,DataSet.Fields[i]);
          end;
        end;
        aj.AsArray.Add(sj2);
        DataSet.Next;
      end;
      sj.O['Data'] := aj;

      Result := sj.AsString;
  finally
      List.Free;
      DataSet.EnableControls;
  end;

end;

Just share my experience using Json using Delphi SuperObject

The idea is how to convert my query result into json format easily.

I use string to represent all data format (including BLOB, DATE, DATETIME). In future the json file can be imported into database easily.

I do not write the code from scratch, i just made a little modification suited into my own need

Here is the code :

function TTableJSon.CreateJsonValueByFieldMySql(Json: ISuperObject;
  Field: TField): Boolean;
var
  JsonTyp, FieldTyp : string;
  tmpStr : string;
begin
  Result := False;
  if Field Is TDateField then begin
      Json.O[Field.FieldName] := SO('"'+FormatDateTime('yyyy-mm-dd',Field.AsDateTime)+'"')
  end else if Field Is TDateTimeField then begin
      Json.O[Field.FieldName] := SO('"'+FormatDateTime('yyyy-mm-dd hh:nn:ss',Field.AsDateTime)+'"')
  end else if Field is TMemoField then begin
      Json.S[Field.FieldName] := EncodeString(Field.AsString)
  end else if Field is TBlobField then begin
      Json.S[Field.FieldName] := EncodeString(Field.AsString)
  end else if Field is TFloatField then begin
      Json.O[Field.FieldName] := SO(ReplaceStr(Field.AsString,',','.'))
  end else begin
        Json.O[Field.FieldName] := SO(Field.Value);
  end;
  Result := True;
end;


function TTableJSon.JSonFromDataSet(DataSet: TDataSet): string;
  procedure GetFieldTypeInfo(Field:TField;var Fieldtyp,JsonTyp:string);
  begin
      Fieldtyp := GetEnumName(TypeInfo(TFieldType),ord(Field.DataType));
      Delete(Fieldtyp,1,2);
      if Field is TStringField then
        JsonTyp := 'string'
      else if Field is TDateTimeField then
        JsonTyp := 'integer'
      else if Field is TMemoField then
        JsonTyp := 'memo'
      else if Field is TBlobField then
        JsonTyp := 'blob'
      else if (Field is TIntegerField) or (Field is TLargeintField) then
        JsonTyp := 'integer'
      else if Field is TCurrencyField then
        JsonTyp := 'currency'
      else if Field is TNumericField then
        JsonTyp := 'double'
      else if Field is TBooleanField then
        JsonTyp := 'boolean'
      else
        JsonTyp := 'variant';
  end;

var
  sj,aj,sj2:ISuperObject;
  i:Integer;
  Fieldtyp,JsonTyp:string;
  List:TStringList;
begin
  sj := SO();
  aj := SA([]);
  List := TStringList.Create;
  try
      List.Sorted := True;

      for i := 0 to DataSet.FieldCount - 1 do
      begin
        sj2 := SO();
        GetFieldTypeInfo(DataSet.Fields[i],Fieldtyp,JsonTyp);

        sj2.S[cstFieldName] := DataSet.Fields[i].FieldName;
        sj2.S[cstFieldType] := Fieldtyp;
        sj2.S[cstJsonType] := JsonTyp;
        sj2.I[cstFieldSize] := DataSet.Fields[i].Size;
        sj2.B[cstRequired] := DataSet.Fields[i].Required;
        sj2.I[cstFieldIndex] := DataSet.Fields[i].Index;
        aj.AsArray.Add(sj2);
        List.Add(DataSet.Fields[i].FieldName+'='+JsonTyp);
      end;
      sj.O['Cols'] := aj;
      DataSet.DisableControls;

      DataSet.First;
      aj := SA([]);
      while not DataSet.Eof do
      begin
        sj2 := SO();
        for i := 0 to DataSet.FieldCount - 1 do
        begin
          if VarIsNull(DataSet.Fields[i].Value) then
            sj2.O[DataSet.Fields[i].FieldName] := SO(Null)
          else begin
            CreateJsonValueByFieldMySql(sj2,DataSet.Fields[i]);
          end;
        end;
        aj.AsArray.Add(sj2);
        DataSet.Next;
      end;
      sj.O['Data'] := aj;

      Result := sj.AsString;
  finally
      List.Free;
      DataSet.EnableControls;
  end;

end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文