我是新來的Python並閱讀了這篇文章,它似乎很容易,但由於某種原因我無法調試錯誤。我猜測這是一件很簡單的......嘗試在Python中加載JSON字符串時出錯
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'
我有它返回一個JSON字符串一個subprocess.call捲曲聲明。
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://[email protected]/","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/ 初始化 py」爲,線326,在負載 回報_default_decoder .decode(s)
文件「/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py」,行360,解碼 obj,end = self .raw_decode(s,idx = _w(s,0).end())
我試圖理解爲什麼會有一個尾隨零,如果錯誤是相關的。如果是的話可以有人建議繞過它,也許C這樣做的直接方法。
謝謝
你顯然試圖加載無效的JSON,最後的零將會打破它。不知道你在'curl語句'中有什麼。 –