【发布时间】:2014-06-14 21:29:24
【问题描述】:
我是 Python 新手,阅读这方面的内容似乎很容易,但由于某种原因,我无法调试该错误。我猜这是非常简单的事情......
2 个函数
def get_json():
return json.load(open('environment.json', 'r'))
def curlopia(j_son=get_json()):
sf_url = j_son['sf_sandbox_url']['url']
grant_type = j_son['oauth_parms']['grant_type']
client_id = j_son['oauth_parms']['client_id']
client_secret = j_son['oauth_parms']['client_secret']
username = j_son['oauth_parms']['username']
password = j_son['oauth_parms']['password']
param = '-d'
我在 subprocess.call 中有一个 curl 语句,它返回一个 json 字符串。
x=subrpocess.call(["curl", sf_url, param, "grant_type=%s" % (grant_type), param, "client_id=%s" % (client_id), param, "client_secret=%s" % (client_secret), param, "username=%s" % (username), param, "password=%s" % (password)])
或
x=os.system('curl {0} -d "grant_type={1}" -d "client_id={2}" -d "client_secret={3}" -d "username={4}" -d "password={5}" -H "X-PrettyPrint:1"'.format(sf_url, grant_type, client_id, client_secret, username, password))
当我打印 x 时,结果是末尾的尾随零。
{"id":"https://blah@blah.blah/","issued_at":"xxxxxxxxxxx","token_type":"Bearer","instance_url":"xxxxxxxxxx","signature":"xxxxxxxxx","access_token":"xxxxxxxxxxxxx"}0
不知道为什么。
当我这样做时
json.loads(x)
给我以下错误。我也尝试过各种组合
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py”,第 326 行,加载中 返回_default_decoder.decode(s)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py”,第 360 行,在 decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())
我试图理解为什么会有一个尾随零以及错误是否与此有关。如果是这种情况,有人可以提出解决方法,也许是正确的方法。
谢谢
【问题讨论】:
-
您显然是在尝试加载无效的 JSON,最后的零会破坏它。不知道你在
curl statement有什么。
标签: json python-2.7