Python json serialization deserialization and Chinese Encoding
In a project, you need to obtain a json-format data from the cloud, save it to a local file, and then read it out. In this case, you need to use the json dumps method and loads method, to achieve data serialization and deserialization. Specifically, the dumps method can set the data sequence in json format to the related data type of python, which is usually used for printing, and the loads method is the opposite, converts the python data type to the corresponding json Data Type format. After deserialization, you can perform various operations on the json data. A problem was also found during programming. During serialization, Chinese characters are always converted to unicode codes. After searching online, you can add the parameter ensure_ascii = False to the dumps function. The experimental procedure is as follows:
Import json; # original json data jsonData = [{'name': 'zhang mou', 'age': 30, 'sex': 'male'}, {'name ': 'Lee mou', 'age': 20, 'sex': 'female}]; # serialize and output jsonStr = json. dumps (jsonData, ensure_ascii = False); print ("serialization Result:"); print (jsonStr); # deserialize jsonStr to json format jsonData = json. loads (jsonStr); print ("Overall deserialization Result:"); print (jsonData); print ("deserialization result traversal using loop:"); for each in jsonData: print (each ['name'], each ['age'], each ['sex']);
Result:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + cost + 8/cost + 0tb294bn7oaM8L3A + CjxwPrHIyOejrL2ryc/Cost/Co7o8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20141111/20141111082353252.png" alt = ">