PHP->Python JSON issue -
what doing wrong? python3:
>>> import json >>> s = "\"{'key': 'value'}\"" >>> obj = json.loads(s) >>> obj['key'] traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: string indices must integers
given json string produced json_encode()
php
real string:
{sessionid:5,file:\/home\/lol\/folder\/folder\/file.ttt}
.
update: problem because transfer json string command line shell_exec
in php, , read using sys.argv[1]
... quotes removed shell.
php code (which runs python script shell_exec
)
$arg = json_encode(['sessionid' => $session->getid(), 'file' => $filename]); shell_exec('python3 script.py ' . $arg);
python code:
import json, sys s = json.loads(sys.argv[1]) #fails
look @ json:
"\"{'key': 'value'}\""
you have string containing escaped quote, followed dictionary-like object, escaped quote. when try decode wind following:
"{'key': 'value'}"
that's string, not dictionary.
Comments
Post a Comment