简体   繁体   English

无法解码来自 PHP 中 Python 的 JSON 字符串

[英]Can't decode JSON string coming from Python in PHP

I have the following Python code:我有以下 Python 代码:

array_to_return = dict()
response_json_object = json.loads(responsestring)

for section in response_json_object:
    if section["requestMethod"] == "getPlayerResources":
        array_to_return["resource_list"] = json.dumps(section["responseData"]["resources"])
        break
array_to_return["requests_duration"] = time.time() - requests_start_time
array_to_return["python_duration"] = time.time() - python_start_time

Which returns the following content into a PHP script:它将以下内容返回到 PHP 脚本中:

{'resource_list': '{"aaa": 120, "bbb": 20, "ccc": 2138, "ddd": 8}', 'requests_duration': '7.30', 'python_duration': 41.0}

I'm then trying to decode this string and convert it into something usable in PHP.然后我试图解码这个字符串并将其转换为 PHP 中可用的东西。 My code if the following:我的代码如下:

$cmd = "$python $pyscript";
exec("$cmd", $output);

echo 'output: ';
var_dump($output[0]);

$json_output = json_decode($output[0], true);

echo 'json_output: ';
var_dump($json_output, json_last_error_msg());

$output[0] is a string but json_last_error_msg() returns Syntax Error $output[0]是一个字符串,但json_last_error_msg()返回Syntax Error

I'm well aware that my string is not a valid Json string, but how can I convert it properly (either in Python or in PHP)?我很清楚我的字符串不是有效的 Json 字符串,但是如何正确转换它(在 Python 或 PHP 中)? I probably do something wrong in my Python script...我可能在我的 Python 脚本中做错了什么......

UPDATE 1:更新 1:

I actually found out that responsestring is a valid JSON string (with double quotes) but json.loads switches the double to single quotes;我实际上发现responsestring是一个有效的 JSON 字符串(带双引号),但json.loads将双引号切换为单引号; thus response_json_object has single quotes.因此response_json_object有单引号。

If I comment out the line with json.loads , I get an error:如果我用json.loads注释掉该行,我会收到错误消息:

TypeError: 'int' object is not subscriptable

UPDATE 2:更新 2:

I managed to get around it by removing the associative list in Python, not exactly what I was hoping for but this works for now...我设法通过删除 Python 中的关联列表来解决它,这并不完全是我所希望的,但这目前有效......

array_to_return = json.dumps(section["responseData"]["resources"])
#No longer using the following
#array_to_return["requests_duration"] = time.time() - requests_start_time
#array_to_return["python_duration"] = time.time() - python_start_time

If a working solution with associative list is suggested, I will accept that one.如果建议使用关联列表的可行解决方案,我将接受该解决方案。

The ' character is not a legal character for JSON, it must be a " . '字符不是 JSON 的合法字符,它必须是"

Your json should look like this.您的 json 应如下所示。

{
    "resource_list": "{\"aaa\": 120, \"bbb\": 20, \"ccc\": 2138, \"ddd\": 8}",
    "requests_duration": "7.30",
    "python_duration": 41.0
}

instead of modifying the individual key, value pairs of array_to_return by json.dumps , you would json.dumps the whole dictionary.而不是通过json.dumps修改array_to_return的单个键值对,您将json.dumps整个字典。

array_to_return = dict()
response_json_object = json.loads(responsestring)

for section in response_json_object:
    if section["requestMethod"] == "getPlayerResources":
        
  
  
    array_to_return["resource_list"] = json.dumps(section["responseData"]["resources"]) 
          array_to_return["resource_list"] = section["responseData"]["resources"]
        break
array_to_return["requests_duration"] = time.time() - requests_start_time
array_to_return["python_duration"] = time.time() - python_start_time

json.dumps(array_to_return)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM