Python Common module JSON

Source: Internet
Author: User

Python jons Module

JSON module is mainly to solve the problem of conversion of data format, such as Python received a JSON object needs to be converted to Python object, for Python processing, or Python data need to be sent to other clients, At this point, you need to convert the Python data to a JSON object for other clients to manipulate.

JSON provides 4 methods, dumps loads dump load


Dumps loads handles all string objects.

Dump load handles the file object

#1, convert a Python object to a JSON object (serialized)
Import Jsondata = {"Name": "Cnblogs", "url": "www.cnblogs.com"}new_data = json.dumps (data) print (new_data) print (Type ( New_data))





#python列表转换为json
Data =[1,2,3]new_data = json.dumps (data) print (type (data)) print (New_data)



#2, convert the JSON object to a Python-processed object (deserialization)


Import JSON
#定一个json对象, you must use quotation marks here, or you will get an error!

data = ' ' {' name ': ' cnblogs ', ' url ': ' www.cnblogs.com '} ' ' Print (type ') New_data = json.loads (data) print (New_data) Print (Type (new_data))


#3, dump converts the Python object to a JSON object and writes the file

Import Jsondata = {"Name": "Cnblogs", "url": "Www.cnblogs.com"}with Open (' Test.txt ', ' W ') as F:    Json.dump (DATA,F)



#4, Laod is read from the file and serialized into a Python object
Import Jsonwith Open (' Test.txt ', ' R ') as F:    data = Json.load (f) print (data) print (type data)

Python Common module JSON

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.