idhttpserver接受json数据,并打印
1 procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 2 var 3 tmp: UTF8String; //也可以是 array of AnsiChar; 不能用RawByteString或 widestring 4 tmpstr: string; 5 size: Integer; 6 JSONObject: TJSONObject; // JSON类 7 i: Integer; 8 jo: ISuperObject; 9 begin 10 size := ARequestInfo.PostStream.size; 11 SetLength(tmp, size); 12 ARequestInfo.PostStream.Position := 0; 13 ARequestInfo.PostStream.ReadBuffer(tmp[1], size); //tmp为array of AnsiChar时为tmp[0] 14 tmpstr := UTF8ToString(tmp); 15 16 jo := SO(tmpstr); 17 Memo1.Lines.Add(jo['test'].AsString); 18 20 kbmMemTable1.Append; 21 kbmMemTable1.FieldByName('test').AsString := jo['test'].AsString; 22 kbmMemTable1.Post; 23 24 // Memo1.Lines.Add(ExtractFilePath(Application.ExeName) + 'testPrint.fr3'); 25 26 frxReport1.LoadFromFile(ExtractFilePath(Application.ExeName) + 'testPrint.fr3'); 27 frxReport1.PrintOptions.ShowDialog := false; 28 frxReport1.ShowProgress := False; 29 frxReport1.PrepareReport; 30 frxReport1.Print; 31 32 // JSONObject := TJSONObject.ParseJSONValue(Trim(tmpstr)) as TJSONObject; 33 // for i := 0 to JSONObject.Count - 1 do 34 // Memo1.Lines.Add(JSONObject.Pairs[i].JsonString.ToString + '=' + 35 // JSONObject.Pairs[i].JsonValue.ToString);
36 end;
我用superobject转换json,也可以用系统自带。主要用IdHTTPServer组件接受http请求。