本文整理自网络,侵删。
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 strtoint 将“字符型”转换成“整数型”
Delphi xe增强的rtti妙用--动态创建包中的窗口类
更多相关阅读请进入《Delphi》频道 >>