• DataSetToJSON


    unit FMX.DataSetToJSON;
    
    interface
     uses
      FireDAC.Comp.Client,Data.DB;
    
      function DataSetToJSON(DataSet:TDataSet):String;
      function JSONToDataSet(JSONTEXT:String):TFDMemTable;
    
    
    implementation
    uses System.Rtti,System.JSON;
    
    
    function DataSetToJSON(DataSet:TDataSet):String;
    var
      I:integer;
      JSONObject,FieldJSONObject:TJSONObject;
      JSONArray:TJSONArray;
    begin
      Result:='';
          JSONObject:=TJSONObject.ParseJSONValue('{}') as TJSONObject;
           try
                  JSONObject.AddPair('Fields',TJSONArray.Create);
                  JSONArray:=JSONObject.GetValue('Fields') as TJSONArray;
                  for I := 0 to DataSet.FieldDefs.Count   -1 do
                  begin
                              FieldJSONObject:=TJSONObject.Create ;
                              FieldJSONObject.AddPair  ('FieldName',DataSet.FieldDefs[i].Name );
                              FieldJSONObject.AddPair('DataType',TRttiEnumerationType.GetName<TFieldType>(DataSet.FieldDefs[i].DataType));
                              FieldJSONObject.AddPair('DataSize',TJSONNumber.Create(DataSet.FieldDefs[i].Size));
                              JSONArray.Add(FieldJSONObject) ;
                  end;
                  DataSet.First ;
                  JSONObject.AddPair('DATAS',TJSONArray.Create);
                  JSONArray:=JSONObject.GetValue('DATAS') as TJSONArray;
                  while not DataSet.Eof  do
                  begin
                      FieldJSONObject:=TJSONObject.Create ;
                      for I := 0 to DataSet.FieldDefs.Count   -1 do
                      FieldJSONObject.AddPair(DataSet.FieldDefs[i].Name,DataSet.Fields[i].AsString);
                      JSONArray.Add(FieldJSONObject) ;
                      DataSet.Next ;
                  end;
                  Result :=  JSONObject.ToJSON ;
           finally
               JSONObject.Free ;
           end;
    end;
    
    
    function JSONToDataSet(JSONTEXT:String):TFDMemTable;
    var
      I,R:integer;
      JSONObject,FieldJSONObject:TJSONObject;
      JSONArray:TJSONArray;
      DataName,DataType:String;
      FieldType:TFieldType;
      FieldSize:integer;
    begin
      Result:=TFDMemTable.Create(nil);
      if JSONTEXT='' then Exit;
           JSONObject:=TJSONObject.ParseJSONValue(JSONTEXT)  as TJSONObject;
           try
                  JSONArray:=JSONObject.GetValue('Fields') as TJSONArray;
                  for I := 0 to JSONArray.size  -1 do
                  begin
                      Result.FieldDefs.Add(((JSONArray.Get(i) as TJSONObject).GetValue('FieldName') as TJSONString).Value,
                      TRttiEnumerationType.GetValue<TFieldType>(((JSONArray.Get(i) as TJSONObject).GetValue('DataType')  as TJSONString).Value),
                      ((JSONArray.Get(i) as TJSONObject).GetValue('DataSize') as TJSONNumber).AsInt64);
                  end;
                  Result.CreateDataSet ;
                  JSONArray:=JSONObject.GetValue('DATAS') as TJSONArray;
                  for I := 0 to JSONArray.size  -1 do
                  begin
                      Result.Append ;
                      for R := 0 to Result.FieldDefs.Count -1  do
                      begin
                          FieldJSONObject:=(JSONArray.Get(i) as TJSONObject);
                          if FieldJSONObject=nil then continue;
    
                          try
                          Result.FieldByName(Result.FieldDefs[R].Name).Value :=
                          (FieldJSONObject.GetValue(Result.FieldDefs[R].Name) as TJSONString).Value ;
    
                          Except
                          end;
                      end;
                      Result.Post ;
                  end;
           finally
               JSONObject.Free ;
           end;
    end;
    end. 
  • 相关阅读:
    javascript基础全等号运算符
    javascript 使用ScriptX实现打印
    跨服务器与本地服务器不同数据库的SQL操作语句
    ASP.NET网络上实现单点登录
    FGMap API 帮助文档
    基于ArcEngine写的GoogleMap地图切割程序
    基于SuperMap Objects写的GoogleMap地图切割程序(三)
    使用SuperSocket开发联网斗地主(四):出牌
    JAVA创建对象方法
    Mysql 外键约束
  • 原文地址:https://www.cnblogs.com/kinglandsoft/p/9254083.html
Copyright © 2020-2023  润新知