我试图将编码为JSON的php数组发送到python脚本,但它不起作用。 我的代码是:
<?php
$data = array('as', 'df', 'gh');
// $result = shell_exec('python /path/to/myScript.py ' . escapeshellarg(json_encode($data)));
$result = system('pythomPath/python scriptPath/myscript.py ' . escapeshellarg(json_encode($data)).' 2>&1',$result);
// Decode the result
$resultData = json_decode($result, true);
// This will contain: array('status' => 'Yes!')
var_dump($resultData);
?>
Python:
^{pr2}$
发送到
json.loads
的数据不是有效的JSON,请检查它。如果要将该数组转换为JSON,只需像在代码末尾那样使用json.dumps
。在但这并不是唯一的错误。如果将此数组作为参数作为系统argv,您将不会得到预期的结果。如果您向这个脚本发送一个数组,它将把这个param作为一个字符串来处理。在
尝试使用这种方法将其作为列表处理,而不是JSON。在
相关问题 更多 >
编程相关推荐