關於requests.post().json()獲取到的JSON為單引號和NONE的那些事


上篇學習sys.argv的使用代碼中發現了錯誤,獲取到的Json數據為單引號和NONE,為錯誤的json格式。

經檢查,發現直接使用r.text就可以得到正確的json數組,使用r.json()反而使json數組中的雙引號變為單引號,null變為NONE。

 原因為.json()會改變正確格式的json數組,需要dump()處理

修改后代碼如下

import requests
import sys

url = 'http://www..com//Order/'


def query(companyid, he_type, he_no):
    d = {'companyid': companyid, 'type': he_type, 'no': he_no}
    r = requests.post(url, data=d)
    return r.text


if __name__ == '__main__':
    company_id = sys.argv[1]
    the_type = sys.argv[2]
    the_no = sys.argv[3]
    print(query(company_id, the_type, the_no))
    # print(query("", 1, ""))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM