本文介绍了PHP json_encode和javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在PHP中将javascript函数编码为JSON对象。
I need to encode a javascript function into a JSON object in PHP.
这个:
$function = "function(){}";
$message = "Hello";
$json = array(
'message' => $message,
'func' => $function
);
echo json_encode($json);
输出:
{"message":"Hello","func":"function(){}"}
我想要的是:
{"message":"Hello","func":function(){}}
我可以用json_encode执行此操作吗?
Can I do this with json_encode?
推荐答案
正如Jani所说,这不可能直接使用JSON,但这可能会对您有所帮助:
As Jani said, this is not possible directly with JSON, but this might help you: http://web.archive.org/web/20080828165256/http://solutoire.com/2008/06/12/sending-javascript-functions-over-json/
这篇关于PHP json_encode和javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!