A method for producing an ordered Chinese python json string

The method of the first with OrderedDict converted into an ordered dict dict

from collections import OrderedDict
data_raw = { "appCode": appcode.upper (), "serviceName": " Service 5", "serviceUrl": " s3", "serviceDesc": " Remarks", "version": "v1 ", "requestMethod": "the GET"} 
Data = OrderedDict ()
Data [ 'AppCode'] = Data_raw [ 'AppCode']
Data [ 'serviceName'] = Data_raw [ 'serviceName']
Data [ 'the serviceUrl'] = Data_raw [ 'the serviceUrl']
Data [ 'serviceDesc'] = Data_raw [ 'serviceDesc']
Data [ 'Version'] = Data_raw [ 'Version']
Data [ 'requestMethod'] = Data_raw [ 'requestMethod']
is then converted into json string will when transformed with json default method automatically converted to unicode, it is necessary to add ensure_ascii = False, the default character set is
Parameter encoding = 'utf-8', so that if a different character encoding, e.g. GBK, it is necessary to add encoding = 'gbk' like
data = json.dumps (data, ensure_ascii = False) 
if the request packet requests to use, in the header plus remember 'charset': 'utf-8 '
Then you can request a success

Guess you like

Origin www.cnblogs.com/slqt/p/11345550.html