简体   繁体   English

使用python从命令行传递的json参数无法解码

[英]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.

相关问题 从Windows命令行运行Python脚本,未传递参数 - Run Python scripts from Windows command line, argument not passed 使用`python -m jsontool`从命令行验证JSON给出'No JSON对象可以被解码' - validating JSON from command line using `python -m jsontool` gives 'No JSON object could be decoded' 有没有一种方法可以在 python 中传递 0 命令行 arguments 并且如果传递了特定参数则具有相同的结果? - Is there a way that I can pass 0 command line arguments in python and have the same result if a particular argument is passed? 如何导入在python中作为命令行参数传递的文件 - how to import file passed as command line argument in python Python : 想要使用在 python 中传递的命令行参数,作为将在 python 脚本中执行的另一个命令的参数 - Python : want to use the command line argument passed in python, as an argument to another command that will be executed within the python script 从Python传递shellcode作为命令行参数 - Passing shellcode from Python as command line argument python 2.7检查是否已从命令提示符传递了参数 - python 2.7 check if argument has been passed from command prompt python的命令行参数 - Command line argument with python Python:JSON 解码库,可以将解码的项目与原始行号相关联? - Python: JSON decoding library which can associate decoded items with original line number? 当列表来自 json 文件时,如何在 Python 中将整个列表作为命令行参数传递 - How to pass an entire list as command line argument in Python when the list comes from a json file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM