JSON--Dump load dumps loads simple comparison

Source: Internet
Author: User



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

Examples are as follows:

>>> import JSON module in JSON #导入python
>>> L = [' Iplaypython ', [n/a], {' name ': ' Xiaoming '}] #创建一个l列表
>>> Encoded_json = Json.dumps (l) # List of L, JSON format encoding
>>> print repr (l)
>>> Print Encoded_json #输出结果

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 Type (Decode_json) #查看一下解码后的对象类型
>>> Print Decode_json #输出结果
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.

Import JSON

data = [{"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 ')
hehe = 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

Transferred from: http://www.cnblogs.com/111testing/p/6032076.html

JSON--Dump load dumps loads simple comparison

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.