Python json serialization deserialization and Chinese Encoding

Source: Internet
Author: User

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 = ">

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.