使用python从命令行传递的json参数无法解码 [英] json argument passed from the command line with python can't be decoded

查看:68
本文介绍了使用python从命令行传递的json参数无法解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将命令行参数传递给python文件并加载json,但是我不断收到值错误:无法解码Json对象.

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.

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}'

即使我在网上找到了这作为正确JSON的示例,也遇到了一个错误.有什么想法吗?

And I get an error, even though I found this as an example of correct JSON on the web. Any ideas?

推荐答案

如果运行 print sys.argv [1] ,则可能会得到'{favorited:json模块无法解码为json对象.

if you run print sys.argv[1] you probably get '{favorited: which the json module cannot decode into a json object.

尝试转义内部引号,以便将其作为1个参数传递,如下所示:

try escaping your inner quotes so it is passed as 1 argument like so:

"{"\""favorited"\"": false, "\""contributors"\"": null}"

这篇关于使用python从命令行传递的json参数无法解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆