Python--json processing

Source: Internet
Author: User

JSON processing:

JSON is a data type that is common in all languages, like a dictionary in Python, and JSON is used with JSON modules, which have the following common methods: Key-value

Import JSON:

  

#dump/dumps Usage: both of the same points are translated into JSON strings, the difference is that the dump is converted into a JSON string after the dictionary is written to the file, such as: Json.dump (OBJ,FP)

One action is to convert "obj" to a JSON-formatted string, and one action is to write a string to a file, meaning that the file descriptor FP is a required parameter

  
DIC = {' name ': ' Liuchengsen ', ' Age ': 18}
Print (Json.dumps (DIC)) #把字典转换成json串
FW = open (' A.json ', ' W ')
res = Json.dump (DIC,FW) #把字段转换成json串并写入到文件中

#load/loads Usage: both of the same points convert the JSON string to a dictionary format, unlike load, which converts a JSON string in a file into a dictionary, such as Json.load ()
Serializes a readable file containing JSON-formatted data into a Python object
S_json = {"Name": "Liuchengsen", "Age": 18}
Print (Json.loads (S_json)) #把json串转换成字典
FR = Open (' A.json ', ' R ')
res = Json.load (FR) #从文件中读取json串 and converted to a dictionary

Python--json processing

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.