关于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, ""))