procedureTServerContainer1.DSHTTPService1FormatResult(Sender: TObject;var ResultVal: TJSONValue; const Command: TDBXCommand; varHandled: Boolean);varstr:string;begin
//str :=ResultVal.ToString;
Handled := true;//加上这句话就可以了。
end;
在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."]
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;
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;
JsonParam.ToString;
4、字符编码
text/html与text/plain
在浏览器打开的时候汉字是乱码,查看网页属性,编码是windows-132,而asp。net生成的webapi网页是utf-8,查找IdCustomHTTPServer.pas
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 belowend;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;
字符编码是charset=ISO-8859-1,改成utf-8就好了!!!
给TDBXParameter赋值
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;
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;