我需要在 PHP 中将 javascript 函数编码为 JSON 对象。
这个:
$function = "function(){}";
$message = "Hello";
$json = array(
'message' => $message,
'func' => $function
);
echo json_encode($json);
输出:
{"message":"Hello","func":"function(){}"}
我想要的是:
{"message":"Hello","func":function(){}}
我可以用 json_encode 做到这一点吗?
最佳答案
正如 Jani 所说,直接使用 JSON 是不可能的,但这可能会对您有所帮助:http://web.archive.org/web/20080828165256/http://solutoire.com/2008/06/12/sending-javascript-functions-over-json/
关于PHP json_encode 和 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1745248/