The solution to reading and writing json Chinese ASCII garbled characters is to read and write jsonascii garbled characters.
Today, I want to help the front end write a small background, that is, read the data and convert it to json for him to display. The data is very simple, but there is a problem during processing. The file involves Chinese processing, and the json format written after each processing is ASCII code, which is totally useless. The Code is as follows:
#-*-Coding: UTF-8-*-import jsonimport codecsf = codecs.open('data.txt ', 'R', 'utf-8') content = json. load (f) print content [0] ['id'] jsdata = json. dumps (content, sort_keys = True, indent = 4) f. close () j = codecs. open ('test. json ', 'w') j. write (jsdata) j. close ()
Check it online. The modified code is as follows:
#-*-Coding: UTF-8-*-import jsonimport sysreload (sys) sys. setdefaultencoding ("UTF-8") f = open('data.txt ', 'R') content = json. load (f) print content [0] ['id'] # concatenate json data and transcode it to non-ascii encoded jsdata = json. dumps (content, sort_keys = True, indent = 4, ensure_ascii = False) f. close () j = open ('test. json ', 'w') j. write (jsdata) j. close ()
The above solution to reading and writing json Chinese ASCII garbled characters is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.