http data text html,DataSnap Server HTTP json格式修改

90505901_1.gif

procedureTServerContainer1.DSHTTPService1FormatResult(Sender: TObject;var ResultVal: TJSONValue; const Command: TDBXCommand; varHandled: Boolean);varstr:string;begin

//str :=ResultVal.ToString;

Handled := true;//加上这句话就可以了。

end;

90505901_1.gif

在Datasnap.DSHTTPCommon.pas,有

procedure TDSJsonResponseHandler.GetCommandResponse

if not Handled  then

ResponseObj := TJSONObject.Create(TJSONPair.Create(TJSONString.Create('result'), JsonParam));的处理,所以可以屏蔽。Handled 设为true就好了。

还有URL编码,特别是汉字,如何能禁止呢??再查。

2、去掉转义字符

元凶在这Datasnap.DSHTTPCommon.pas,TDBXJSONTools.DBXToJSON,

'["[{\"PID\":\"0001\",\"Name\":\"\u5F20\u4E09\u4E30\",\"Sex\":\"\u7537\",\"Age\":100},{\"PID\":\"002\",\"Name\":\"\u90ED\u9756\",\"Sex\":\"\u7537\",\"Age\":80}]"]'

JsonParam :=TDBXJSONTools.DBXToJSON(Command.Parameters[I].Value,

Command.Parameters[I].DataType, FDSService.LocalConnection);

//用下面这句话就可以了,DBXToJSON引起的。

JsonParam := TJSONObject.ParseJSONValue(Command.Parameters[I].Value.AsString) as TJSONValue;

TJSONString

2016.10.11 做了二次修改,兼容单个键值,数值值。例如,进返回时间["2012.0.0.0."]

90505901_2.gif

GetCommandResponse

3、 防止汉字转成unicode数字

[{"PID":"0001","Name":"张三丰","Sex":"男","Age":100},{"PID":"002","Name":"郭靖","Sex":"男","Age":80}]

通过浏览器打开URL,返回的是

[[{"PID":"0001","Name":"\u5F20\u4E09\u4E30","Sex":"\u7537","Age":100},{"PID":"002","Name":"\u90ED\u9756","Sex":"\u7537","Age":80}]]

Datasnap.DSHTTP.pasfunctionPopulateContent

ResponseInfo.ContentText := StringOf(ByteContent(Response))

改为:

ResponseInfo.ContentText := Response.ToString;

Response.Value 为空的原因是结构不一致不,从tjson读取后看value不为空,那么返回value测试一下不。或者对于 Command.Parameters[I].DataType是字符的特殊处理。

CharSet

IdCustomHTTPServer.pas

FConnection.IOHandler.Write(ContentText, CharsetToEncoding(CharSet));

IdCustomHTTPServer.pas

ResponseInfo.ContentText 这个就是返回字符串,

ResponseInfo.ContentText :=  Response.ToString;//      Response.ToJSON;

ResponseInfo.ContentText :=   AnsiReplaceStr(ResponseInfo.ContentText,  '\"','"');

还是因为这个引起的,System.JSON.pas

function TJSONString.ToString: string;

begin

if FStrBuffer = nil then

Result := ''

else

Result := '"' + AnsiReplaceStr(FStrBuffer.ToString, '"', '\"') + '"';

end;

90505901_1.gif

varjo: TJSONObject;

Command: TDBXCommand;

pa: TDBXParameter;

JsonParam: TJSONValue;beginpa := TDBXParameter.Create(nil);

pa.DataType :=TDBXDataTypes.AnsiStringType;

pa.Value.AsString := '[{"name":"david"}]';

JsonParam :=TDBXJSONTools.DBXToJSON(pa.Value, pa.DataType, false);

self.Caption := JsonParam.Value;

90505901_1.gif

JsonParam.ToString;

4、字符编码

text/html与text/plain

在浏览器打开的时候汉字是乱码,查看网页属性,编码是windows-132,而asp。net生成的webapi网页是utf-8,查找IdCustomHTTPServer.pas

90505901_1.gif

procedureTIdHTTPResponseInfo.WriteHeader;vari: Integer;

LBufferingStarted: Boolean;begin

if HeaderHasBeenWritten then begin

raise EIdHTTPHeaderAlreadyWritten.Create(RSHTTPHeaderAlreadyWritten);end;

FHeaderHasBeenWritten :=True;if AuthRealm <> '' then

beginResponseNo := 401;if (Length(ContentText) = 0) and (not Assigned(ContentStream)) then

beginContentType := 'text/html; charset=utf-8'; {Do not Localize}ContentText := '

' + IntToStr(ResponseNo) + ' ' + ResponseText + ''; {Do not Localize}ContentLength := -1; //calculated below

end;end;//RLebeau 5/15/2012: for backwards compatibility. We really should

//make the user set this every time instead...

if ContentType = '' then begin

if (ContentText <> '') or (Assigned(ContentStream)) then beginContentType := 'text/html; charset=ISO-8859-1'; {Do not Localize}//ContentType := 'text/plain; charset=utf-8';

end;end;

90505901_1.gif

字符编码是charset=ISO-8859-1,改成utf-8就好了!!!

给TDBXParameter赋值

90505901_1.gif

procedureTForm4.Button1Click(Sender: TObject);varLDBXParameter: TDBXParameter;

LJSONParam: TJSONValue;

pin: TDBXParameter;beginLDBXParameter := TDBXParameter.Create();

pin := TDBXParameter.Create();

pin.DataType :=TDBXDataTypes.AnsiStringType;

pin.Value.AsString := '[{"PatientID":"aaaaaaaaa","PictuerName":"BBBBBBBBBBB"}]';

LJSONParam :=TDBXJSONTools.DBXToJSON(pin.Value, pin.DataType, false);//下面这句不行

//LJSONParam := TJSONObject.ParseJSONValue('[{"PatientID":"aaaaaaaaa","PictuerName":"BBBBBBBBBBB"}]');

LDBXParameter.DataType :=TDBXDataTypes.WideStringType;

TDBXJSONTools.JSONToDBX(LJSONParam, LDBXParameter.Value, LDBXParameter.DataType, true, true(*Owns*));

Memo1.Lines.Text :=LDBXParameter.Value.AsStringend;

90505901_1.gif

Datasnap.DSService.pas

procedure TDSRESTService.ProcessParameters(const ADSMethodName: string; const Params: TStrings; Content: TArray; const ACommand: TDBXCommand);

dataSnap服务器获取get、post方法参数

URL格式

http://localhost:8023/datasnap/rest/TServerMethods1/EchoString?p1=p1325325&p2=anme

uses Data.DBXPlatform;

function TServerMethods1.EchoString(Value: string): string;beginp1 := GetInvocationMetadata.QueryParams.Values['p1'];

p2 := GetInvocationMetadata.QueryParams.Values['p2'];end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值