Python JSON module dumps loads

Source: Internet
Author: User

The use of JSON data in Python.

Dumps and loads also need to be paired, just like C + + New/delete Malloc/free need to be used in pairs.

Looking at a string like JSON, it's not necessarily a JSON string. Ha ha.

Let's take a look at examples.

1 #Coding=utf-82 3 ImportJSON4 5data = {"name":"Zhang San"," Age": 18,"friend":["Pock","John Doe"]}6 PrintData7 Printjson.dumps (data)8 PrintJson.dumps (data, indent=4)##设置输出格式9 PrintJson.dumps (data, indent=4, Ensure_ascii=false)##非二进制编码Ten  One Print "*"* 50 ADATA_STR = json.dumps (data, indent=4, ensure_ascii=False) -Data_js =json.loads (DATA_STR) - Printdata_js[" Age"]##读取元素 the PrintData_js.get ("Chengshi","BJ")##如果读取不到可以设置默认值

Output:

Python JSON module dumps loads

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.