使用json序列
使用json序列
引用日志单元:
uses core.log;
json对象的接口:
TJSONObjectHelper = class Helper for TJSONObject private procedure SetB(const AKey: str; const AValue: bool); procedure SetD(const AKey: str; const AValue: Double); procedure SetI64(const AKey: str; const AValue: Int64); procedure SetO(const AKey: str; const AValue: TJSONObject); procedure SetS(const AKey, AValue: str); procedure SetA(const AKey: str; const AValue: TJSONArray); function GetI(const AKey: str): int; procedure SetI(const AKey: str; const AValue: int); public function Exists(const AKey: str): bool; procedure Del(const AKey: str); // delete a item function ToUtf8String: str; function ToStr: string; private function GetB(const AKey: str): bool; function GetD(const AKey: str): Double; function GetI64(const AKey: str): Int64; function GetS(const AKey: str): str; function GetA(const AKey: str): TJSONArray; function GetO(const AKey: str): TJSONObject; public property S[const key: str]: str read GetS write SetS; property I[const key: str]: int read GetI write SetI; property I64[const key: str]: Int64 read GetI64 write SetI64; property D[const key: str]: Double read GetD write SetD; property B[const key: str]: bool read GetB write SetB; property O[const key: str]: TJSONObject read GetO write SetO; property A[const key: str]: TJSONArray read GetA write SetA; end; //json string to TJSONObject function SO(const AValue: str): TJSONObject; //json string to TJSONArray function SA(const AValue: str): TJSONArray; //json file to TJSONObject function FromFile(const AFileName: str): TJSONObject; //json file to TJSONArray function FromFile2(const AFileName: str): TJSONArray;
使用方法,请看代码中的注释:
procedure TDanWei.Select(AContext: TContext); var LDB: TDB; LPool: TDBPool; LRequest, LResponse: TJsonO;//json对象 begin LRequest := SO(AContext.InContent);//生成一个json对象,从json字符串 if LRequest = nil then Exit; LResponse := TJsonO.Create;//创建json对象 try try LPool := GetDBPool(LRequest.S[TConst.DBId]); LDB := LPool.Lock; LDB.Select('select * from tunit'); LResponse.B[TConst.Success] := True;//给json对象赋值 LResponse.S[TConst.Msg] := '查询成功'; LResponse.A[TConst.Fields] := LDB.qry.FieldsToJsonArray; LResponse.A[TConst.Data] := LDB.qry.DataToJsonArray; AContext.Send(LResponse); except on E: Exception do begin LResponse.B[TConst.Success] := False; LResponse.S[TConst.Msg] := E.Message; AContext.Send(LResponse);//发送一个json对象 WriteLog('TDanWei.Select()' + E.Message); end; end; finally LPool.Unlock(LDB); LResponse.Free; LRequest.Free; end; end;
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/18853403