Python note-dumps () and the use of loads ()

Source: Internet
Author: User

Json.dumps is the encoding parsing of a Python data type list in JSON format,

Examples are as follows:

Import # import JSON modules in Python # Create an L list # A list of L, JSON format encoding Print repr (L) Print # Output Results

This allows us to convert a list object into a JSON-formatted encoding.

Decoding python JSON format, you can use the module's json.loads () function parsing method,
Examples are as follows:

>>> Decode_json = json.loads (encoded_json)print# Look at the decoded object type  Print# Output Results

Decode python JSON format into Python data style

Json.dump and Json.dumps are very different, json.dump is mainly used to read and write JSON files, and Json.load functions in conjunction with.

Json.dump (X,F), X is an object, and F is a file object that can write a JSON string to a text file.

ImportJsondata= [{"a":"AAA","b":"BBB","C": [4,5,6, (]},33),'TANTENGVIP', True]data2=json.dumps (data)Print(data2) F= Open ('./tt.txt','a') Json.dump (data2,f)

This generates a tt.txt file that holds the JSON-formatted data. The dumps also provides pritty print, formatted output.

Json.load loading JSON format file The following is the JSON data read from the TXT file.
f = open ('./tt.txt','r'= json.load (f)  Print(hehe)

Summarize:

Json.dumps:dict to Str json.dump is to save Python data as JSON

Json.loads:str turns into Dict json.load is to read JSON data

Python note-dumps () and the use of 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.