乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      delphi 數(shù)據(jù)集與JSON對(duì)象互轉(zhuǎn)

       獨(dú)孤求財(cái) 2012-03-12

      delphi 數(shù)據(jù)集與JSON對(duì)象互轉(zhuǎn)

      時(shí)間:2011-6-2來(lái)源:yang 作者: peng點(diǎn)擊: 30次

      在delphi中,數(shù)據(jù)集是最常用數(shù)據(jù)存取方式。因此,必須建立JSON與TDataSet之間的互轉(zhuǎn)關(guān)系,實(shí)現(xiàn)數(shù)據(jù)之間通訊與轉(zhuǎn)換。值得注意的是,這只是普通的TDataset與JSON之間轉(zhuǎn)換,由于CDS包含了Delta數(shù)據(jù)包,其數(shù)據(jù)格式遠(yuǎn)比普通的TDataset更復(fù)雜。
       
      數(shù)據(jù)集字段信息,是一個(gè)完整的字典信息。因此,我們?cè)贘SON必須也建立字典信息,才能創(chuàng)建數(shù)據(jù)集的字段信息。我們?cè)O(shè)置其JSON信息如下:
         COLS:[字段列表信息],如:
      "Cols":[{"JsonType":"integer","FieldIndex":0,"FieldType":"Integer","FieldSize":0,"FieldName":"ID","Required":false},{"JsonType":"string","FieldIndex":1,"FieldType":"String","FieldSize":100,"FieldName":"Title","Required":false},{"JsonType":"variant","FieldIndex":2,"FieldType":"Blob","FieldSize":0,"FieldName":"Picture","Required":false}]
      數(shù)據(jù)信息以Data做節(jié)點(diǎn),也是一個(gè)數(shù)組嵌套記錄信息:
      "Data":[記錄集信息]
      廢話(huà)少說(shuō),直接上代碼:
      unit uDBJson;
      interface
      uses
      SysUtils,Classes,Variants,DB,DBClient,SuperObject;
      type
      TTableJSon = class
      private
          const cstFieldType = ‘FieldType‘;
          const cstFieldName = ‘FieldName‘;
          const cstFieldSize = ‘FieldSize‘;
          const cstJsonType = ‘JsonType‘;
          const cstRequired = ‘Required‘;
          const cstFieldIndex = ‘FieldIndex‘;
          const cstCols= ‘Cols‘;
          const cstData= ‘Data‘;
      public
          class function JSonFromDataSet(DataSet:TDataSet):string;
          class function CreateFieldByJson(Fields:TFieldDefs;ColsJson:ISuperObject):Boolean;
          class function ImportDataFromJSon(DataSet:TDataSet;DataJson:ISuperObject):Integer;
          class function CDSFromJSon(CDS:TClientDataSet;Json:ISuperObject):Boolean;
          class function GetValue(Json:ISuperObject;const Name:string):Variant;
          class function CreateJsonValue(Json:ISuperObject;const Name:string;const Value:Variant):Boolean;
          class function CreateJsonValueByField(Json:ISuperObject;Field:TField):Boolean;
          class function GetValue2Field(Field:TField;JsonValue:ISuperObject):Variant;
      end;
      implementation
      uses TypInfo,encddecd;
      { TTableJSon }
      class function TTableJSon.CDSFromJSon(CDS: TClientDataSet;
      Json: ISuperObject): Boolean;
      var
      ColsJson:ISuperObject;
      begin
      Result := False;
      if Json = nil then
          Exit;
      CDS.Close;
      CDS.Data := Null;
      //創(chuàng)建字段
      ColsJson := Json.O[cstCols];
      CreateFieldByJson(CDS.FieldDefs,ColsJson);
      if CDS.FieldDefs.Count >0 then
          CDS.CreateDataSet;
      ImportDataFromJSon(CDS,Json.O[cstData]);
      Result := True;
      end;
      class function TTableJSon.CreateFieldByJson(Fields: TFieldDefs;
      ColsJson: ISuperObject): Boolean;
      var
      SubJson:ISuperObject;
      ft:TFieldType;
      begin
      Result := False;
      Fields.DataSet.Close;
      Fields.Clear;
      for SubJson in ColsJson do
      begin
          ft := TFieldType(GetEnumValue(TypeInfo(TFieldType),‘ft‘+SubJson.S[cstFieldType]));
          if ft= ftAutoInc then //自增字段不能錄入,必須更改
            ft := ftInteger;
          Fields.Add(SubJson.S[cstFieldName],ft,SubJson.I[cstFieldSize],SubJson.B[cstRequired]);
      end;
      Result := True;
      end;
      class function TTableJSon.CreateJsonValue(Json: ISuperObject;
      const Name: string; const Value: Variant): Boolean;
      begin
      Result := False;
      Json.O[Name] := SO(Value);
      Result := True;
      end;
      class function TTableJSon.CreateJsonValueByField(Json: ISuperObject;
      Field: TField): Boolean;
      begin
      Result := False;
      if Field Is TDateTimeField then
          Json.O[Field.FieldName] := SO(Field.AsDateTime)
      else if Field is TBlobField then
          Json.S[Field.FieldName] := EncodeString(Field.AsString)
      else
          Json.O[Field.FieldName] := SO(Field.Value);
      Result := True;
      end;
      class function TTableJSon.GetValue(
      Json: ISuperObject;const Name: string): Variant;
      begin
      case Json.DataType of
          stNull: Result := Null;
          stBoolean: Result := Json.B[Name];
          stDouble: Result := Json.D[Name];
          stCurrency: Result := Json.C[Name];
          stInt: Result := Json.I[Name];
          stString: Result := Json.S[Name];
      end;
      end;
      class function TTableJSon.GetValue2Field(Field: TField; JsonValue:ISuperObject): Variant;
      begin
      if JsonValue.DataType = stNull then
          Result := Null
      else if Field is TDateTimeField then
          Result := JavaToDelphiDateTime(JsonValue.AsInteger)
      else if (Field is TIntegerField) or (Field is TLargeintField) then
          Result := JsonValue.AsInteger
      else if Field is TNumericField then
          Result := JsonValue.AsDouble
      else if Field is TBooleanField then
          Result := JsonValue.AsBoolean
      else if Field is TStringField then
          Result := JsonValue.AsString
      else if Field is TBlobField then
          Result := DecodeString(JsonValue.AsString)   
      end;
      class function TTableJSon.ImportDataFromJSon(DataSet: TDataSet;
      DataJson: ISuperObject): Integer;
      var
      SubJson:ISuperObject;
      i:Integer;
      iter: TSuperObjectIter;
      begin
      if not DataSet.Active then
          DataSet.Open;
      DataSet.DisableControls;
      try
          for SubJson in DataJson do
          begin
            DataSet.Append;
            if ObjectFindFirst(SubJson,iter) then
            begin
               repeat
                 if DataSet.FindField(iter.Ite.Current.Name)<>nil then
                   DataSet.FindField(iter.Ite.Current.Name).Value :=
                      GetValue2Field(
                      DataSet.FindField(iter.Ite.Current.Name),
                      iter.Ite.Current.Value);
               until not ObjectFindNext(iter) ;
            end;
            DataSet.Post;
          end;
      finally
          DataSet.EnableControls;
      end;
      end;
      class 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 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();
      //創(chuàng)建列
      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;
          //創(chuàng)建數(shù)據(jù)集的數(shù)據(jù)
          DataSet.DisableControls;
          DataSet.First;
          aj := SA([]);
          while not DataSet.Eof do
          begin
            sj2 := SO();
            for i := 0 to DataSet.FieldCount - 1 do
            begin
              //sj2.S[IntToStr(DataSet.Fields[i].Index)] := VarToStrDef(DataSet.Fields[i].Value,‘‘);
              if VarIsNull(DataSet.Fields[i].Value) then
                sj2.O[DataSet.Fields[i].FieldName] := SO(Null)
              else
              begin
                CreateJsonValueByField(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;
      end.
      調(diào)用示例:
      //數(shù)據(jù)集轉(zhuǎn)JSON對(duì)象或JSON文本
      var
      json:TTableJSon;
      s:string;
      begin
      S := json.JSonFromDataSet(ADODataSet1);
      //在用TStringStream讀入字符串S,存成文本,看看其格式.
      end;
      //JSON對(duì)象或文本,裝載到數(shù)據(jù)集
      var
      json:ISuperObject;
      begin
      json := TSuperObject.ParseFile(‘json.txt‘,False);
      TTableJSon.CDSFromJSon(cdsJSON,json);
      end;
       

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多