将php数组传递给python不会

2024-06-26 14:06:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将编码为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}$

Tags: 代码py脚本json编码dataas数组
1条回答
网友
1楼 · 发布于 2024-06-26 14:06:11

发送到json.loads的数据不是有效的JSON,请检查它。如果要将该数组转换为JSON,只需像在代码末尾那样使用json.dumps。在

但这并不是唯一的错误。如果将此数组作为参数作为系统argv,您将不会得到预期的结果。如果您向这个脚本发送一个数组,它将把这个param作为一个字符串来处理。在

尝试使用这种方法将其作为列表处理,而不是JSON。在

data = eval(sys.argv[1])[0]

print (json.dumps(data))

相关问题 更多 >