Python 3.7 字典轉(zhuǎn)json字符串 字典排序 中文亂碼 字符中去掉空格
json.dumps({"b": 2020, "a": "你好"}, ensure_ascii=False, sort_keys=False, separators=(',', ':'))
ensure_ascii=False, 中文字符不用unicode編碼顯示. If
ensure_ascii
is false, then the return value can contain non-ASCII characters if they appear in strings contained inobj
. Otherwise, all such characters are escaped in JSON strings.sort_keys=False, dict中的key不做排序. If
sort_keys
is true (default:False
), then the output of dictionaries will be sorted by key.separators, 指定dumps之后字符拼接分隔符,默認(rèn)為
( ', ', ': ')
,分隔符中帶空格,例如{"a": "2020", "b": 1}批钠;指定 separators=(',', ':')
以后,結(jié)果為{"a":"2020","b":1}If specified,
separators
should be an(item_separator, key_separator)
tuple. The default is(', ', ': ')
if indent isNone
and(',', ': ')
otherwise. To get the most compact JSON representation, you should specify(',', ':')
to eliminate whitespace.應(yīng)用場景: json.dumps之后, 需要計(jì)算md5或者base64的情況下會(huì)有影響.