Delphi调用JavaScript解析JSON

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