delphi josn php,delphi idhttp post json 上传 PHP 接收

前提:PHP端用$_post 接收

一、delphi idhttp post 上传数据 时如果使用 temp_stream: TStringStream;  内存流的方式上传 则需要指明IDHTTP的contentype

var

idhttp2: TIdHTTP;

temp_stream: TStringStream;

temp_url: string;

temp_str: string;

strlist_para:TStringList;

begin

strlist_para:=TStringList.Create();

idhttp2 := TIdHTTP.Create(nil);

strlist_para.Add('xx={"i":"5","billids":[{"id":1},{"id":2},{"id":3}]}') ;

idhttp2.ReadTimeout := 10000; // 请求超时设置

idhttp2.Request.ContentType := 'application/x-www-form-urlencoded';

temp_stream := TStringStream.Create(AnsiToUtf8('xx='+json.AsString)); // 创建一个包含JSON数据的变量

temp_stream.Position := 0; // 将流位置置为0

temp_stream.SaveToFile('c:\123.txt');

temp_stream.Position := 0; // 将流位置置为0

temp_url := '地址';

try

temp_str := idhttp2.Post(temp_url, temp_stream);

except

temp_str := '';

end;

idhttp2.Free();

temp_stream.Free;

result := temp_str;

二、strlist_para:TStringList;方式上传

var

idhttp2: TIdHTTP;

temp_stream: TStringStream;

temp_url: string;

temp_str: string;

strlist_para:TStringList;

begin

strlist_para:=TStringList.Create();

idhttp2 := TIdHTTP.Create(nil);

strlist_para.Add('xx={"i":"5","billids":[{"id":1},{"id":2},{"id":3}]}') ;

idhttp2.ReadTimeout := 10000; // 请求超时设置

idhttp2.Request.ContentType := 'application/x-www-form-urlencoded';

temp_stream := TStringStream.Create(AnsiToUtf8('xx='+json.AsString)); // 创建一个包含JSON数据的变量

temp_stream.Position := 0; // 将流位置置为0

temp_stream.SaveToFile('c:\123.txt');

temp_stream.Position := 0; // 将流位置置为0

temp_url := '地址';

try

temp_str := idhttp2.Post(temp_url, strlist_para);

except

temp_str := '';

end;

idhttp2.Free();

temp_stream.Free;

result := temp_str;

————————————————

原文链接:https://blog.csdn.net/weixin_44387646/article/details/103709801

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一款强大的编程语言,可用于开发各种Windows应用程序。而JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于跨平台数据传输和存储。在Delphi中,我们可以通过JSON来处理数据。 Delphi提供了一些JSON处理库和组件,可以帮助我们在应用程序中使用JSON。其中一个常用的库是SuperObject,它可以方便地对JSON进行解析和构建。 使用Delphi编写JSON示例程序的步骤如下: 1. 首先,需要在Delphi中引入SuperObject库。可以从官方网站下载安装文件,并在Delphi的工具选项中添加库的路径。 2. 创建一个新的控制台应用程序项目。 3. 在代码中,引入SuperObject单元,并使用TJSONObject类来创建一个JSON对象。 4. 可以使用JSON对象的方法来添加、修改和删除JSON数据。 5. 使用TJSONObject的ToString方法将JSON对象转换为字符串,并通过ShowMessage或WriteLn函数来显示JSON数据。 下面是一个简单的Delphi JSON示例程序: ```Delphi program DelphiJsonDemo; {$APPTYPE CONSOLE} uses SysUtils, SuperObject; var jsonObj: ISuperObject; begin // 创建一个JSON对象 jsonObj := TSuperObject.Create; // 向JSON对象中添加数据 jsonObj.S['name'] := 'John'; jsonObj.I['age'] := 25; jsonObj.B['isStudent'] := True; // 将JSON对象转换为字符串并显示 Writeln(jsonObj.AsJSON); Readln; end. ``` 以上代码创建了一个包含姓名、年龄和是否为学生的JSON对象,并将其转换为字符串并显示出来。 通过这个简单的示例,我们可以看到Delphi中处理JSON数据的基本步骤。该程序只是一个简单的演示,实际应用中可能需要更复杂的JSON处理操作。但是,掌握了基本的概念和库的使用方法后,我们就能在Delphi应用程序中灵活地处理JSON数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值