[英]json argument passed from the command line with python can't be decoded
I am trying to pass a command line argument to a python file and load the json but I keep getting a valueerror: No Json object could be decoded. 我正在尝试将命令行参数传递给python文件并加载json,但我一直遇到valueerror:无法解码Json对象。
json_test.py: json_test.py:
import json
import sys
json_dict = json.loads(sys.argv[1])
I then run this in the command line using: 然后,我在命令行中使用以下命令运行它:
python json_test.py '{"favorited": false, "contributors": null}'
And I get an error, even though I found this as an example of correct JSON on the web. 即使我发现这是一个在网络上正确的JSON的示例,但我还是得到一个错误。 Any ideas?
有任何想法吗?
if you run print sys.argv[1]
you probably get '{favorited:
which the json module cannot decode into a json object. 如果运行
print sys.argv[1]
,可能会得到'{favorited:
json模块无法将其解码为json对象。
try escaping your inner quotes so it is passed as 1 argument like so: 尝试转义您的内部引号,以便将其作为1参数传递,如下所示:
"{"\""favorited"\"": false, "\""contributors"\"": null}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.