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下idhttp配合cookiemanager获取cookie

Delphi 获取cpu monitor使用率

Delphi通过代码实现模拟按键的函数

Delphi 获取计算机从开机开始的已运行时间

Delphi webbrowser ie 窗口选择中,选中的文字内容

Delphi firedac连接mysql的时候报错

Delphi 通过递归来实现搜索文件

Delphi 如何知道鼠标点击的statusbar的位置

Delphi中inputbox 和inputquery 函数的使用

Delphi xe(indy10)tidbytes转ansistring的实现

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



打赏

取消

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

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

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

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

评论

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