Delphi调用JavaScript解析JSON


本文整理自网络,侵删。

 

1.首先加入ComObj单元
2.加入函数
functin RunJs(const JsCode,JsVar:string):string;
var
 script:OleVariant;
begin
 try
  Script :=CreateOleObject('ScriptControl');
  Script.Language :='JavaScript';
  Script.ExecuteStatement(JsCode);
  Result :=Script.Eval(JsVar);
 except
   result :='';
 end;
end; 

在Button1的OnClick事件中写入
S :=RunJs(Memo1.Text,'str');
Showmessage(S);

在Memo1中写入str=1+2,弹出3
如果写入以下JSON解析函数,则弹出解析后的结果
function ShowJson() {
 var UserList=[
  {"UserID":11,"Name":{"FirstName":"Trudy","LastName":"L1"},
  {"UserID":11,"Name":{"FirstName":"F1","LastName":"L2"}}
 ];
 return UserList[0].Name.FirstName;
}
str=ShowJson();

则弹出Trudy

或者用FastScript也可以解析,功能更强,可参考网站http://www.fast-report.com/pbc_download/files/fs_en.pdf

相关阅读 >>

Delphi 操作“任务栏”

Delphi textfile utf8编码读写

Delphi10.2.3解决的一个json问题

Delphi date 返回当前的日期

Delphi xe2 将域名转ip

Delphi winapi: getparent、setparent、movewindow - 获取、指定父窗口和移动窗口

Delphi pchar与string互转

Delphi 根据ip获取局域网mac

Delphi xe将图标和图像添加为资源

Delphi mediaplayer 左声道右声道

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...