python在做接口测试的时候,打印出来的json格式的中文数据显示异常
例如:
def test_bookOrder(self):
self.url="http://mobapi.senbaba.cn/bookOrder"
self.data={"confirm":"0","withTolls":"false","destinationPlace":"深圳北站","tdays":"0","destinationCoords":"{'area':',深圳市,宝安区,','coords':[22.616128,114.037994],'name':'深圳北站'}","departureCoords":"{'area':',广州市,越秀区,','coords':[23.155001,113.264057],'name':'广州火车站'}","departurePlace":"广州火车站","etBeginTime":"2016-07-13 15:52:00","pathway":"[]","vehicleTypeCode":"VT0001","jobType":"0","remark":"test","couponCode":"","count":"1","pathway":"[]","addItion":"false","components":"","contractPhone":"15112408147","tripNo":"test","tolls":"0","contractName":"test","cost":"620","etFinishTime":"2016-08-30 19:23:18"}
z=requests.post(self.url,self.data)
print(z.json())
打印出来的消息显示的是:{u'status': u'failed', u'error': {u'msg': u'\u8bf7\u767b\u9646\u540e\u8fdb\u884c\u64cd\u4f5c!', u'code': u'err_needlogin'}}
发现全是中文uncode显示
解决方法:
def test_bookOrder(self):打印出来的消息显示为:{"status": "failed", "error": {"msg": "请登陆后进行操作!", "code": "err_needlogin"}}
self.url="http://mobapi.senbaba.cn/bookOrder"
self.data={"confirm":"0","withTolls":"false","destinationPlace":"深圳北站","tdays":"0","destinationCoords":"{'area':',深圳市,宝安区,','coords':[22.616128,114.037994],'name':'深圳北站'}","departureCoords":"{'area':',广州市,越秀区,','coords':[23.155001,113.264057],'name':'广州火车站'}","departurePlace":"广州火车站","etBeginTime":"2016-07-13 15:52:00","pathway":"[]","vehicleTypeCode":"VT0001","jobType":"0","remark":"test","couponCode":"","count":"1","pathway":"[]","addItion":"false","components":"","contractPhone":"15112408147","tripNo":"test","tolls":"0","contractName":"test","cost":"620","etFinishTime":"2016-08-30 19:23:18"}
z=requests.post(self.url,self.data)
change=z.json()
new=json.dumps(change, ensure_ascii=False)
print(new)
通过json库里面的dumps方法可以让其显示为中文,默认是以ASCII来解析code,中文不在ASCII编码当中,所以无法正常显示。json.dumps方法讲ASCII编码设置为false即可解决此问题。