我在将数组分配给 JSON 时遇到了麻烦(我是 python 的初学者)。例如,在使用 python 控制台时它工作正常 -
>>> data = [
... "byte_array_format",
... "auto_timer",
... "auto_calc",
... "auto_intense",
... "auto_balance"]
>>> print(data)
['byte_array_format', 'auto_timer', 'auto_calc', 'auto_intense', 'auto_balance']
>>> json_db = {}
>>> json_db["value"] = data
>>> print(json_db)
{'value': ['byte_array_format', 'auto_timer', 'auto_calc', 'auto_intense', 'auto_balance']}
虽然我在使用 python 代码时做同样的事情,但它为方括号加上单引号。
json_obj = json.dumps(unique_ids)
print(json_obj)
["byte_array_format", "auto_timer", "auto_calc", "auto_intense", "auto_balance"]
添加json_obj为value方括号添加单引号,这最终导致无效 JSON。
final_json = {}
final_json["value"] = json_obj
print(final_json)
{'value': '["byte_array_format", "auto_timer", "auto_calc", "auto_intense", "auto_balance"]'}