PHP-> Python JSON问题

What am I 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 be integers

Given JSON string is produced by json_encode() from php

Real string: {sessionid:5,file:\/home\/lol\/folder\/folder\/file.ttt}.

UPDATE: Problem is because i transfer json string as command line to shell_exec in php, and read it using sys.argv[1]... So quotes are removed by shell.

PHP Code (which runs python script with 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 already

Kasra answer is correct, ast does the trick, but, additionally i had to 'fix' my string with php - to prevent 'damaging' (by shell?):

php code:

$cmd = json_encode(['sessionid' => $session->getId(), 'file' => $filename]);
$cmd = str_replace('"', '\'', $cmd);
$cmd = str_replace('\\/', '/', $cmd);
$cmd = 'python3 program.py "' . $cmd . '"';
exec($cmd);

python code:

import json, sys, ast  
task = json.loads("\"" + sys.argv[1] + "\"")
task = ast.literal_eval(task)

sessionid = task['sessionid'] # DONE!:)

Look at this JSON:

"\"{'key': 'value'}\""

You have a string containing an escaped quote, followed by a dictionary-like object, then an escaped quote. When you try to decode this you wind up with the following:

"{'key': 'value'}"

That's a string, not a dictionary.

As your string surrounded by 2 quote that you escaped one of them so after reading the string with json the result will be string,So after that you can use ast.literal_eval to convert your string to dictionary :

>>> obj = json.loads(s)
>>> obj
"{'key': 'value'}"
>>> import ast
>>> new=ast.literal_eval(obj)
>>> new['key']
'value'