Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to deal with json module in Python

Shulou Source: shulou.com Published: 2022-06-02 07:13:43 10月04日 Update

Today, I will talk to you about how to deal with the json module in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

JSON:JavaScript Object Notation [JavaScript object representation]

JSON is a lightweight data exchange format that is completely independent of the text format of any programming language. Generally, the background application encapsulates the response data and returns it in JSON format.

The basic syntax of JSON is as follows: JSON name / value pair. The writing format of JSON data is: name / value pair. The name / value pair includes the field name (in double quotes), followed by a colon (:), and finally the value.

The most commonly used format of JSON is the key-value pair of an object: key can only be string, and value can be object, array, string, number, true/false, null

{"sites": [{"name": "name", "url": "www.360.com"}, {"name": "google", "url": "www.google.com"}, {"name": "baidu", "url": "www.baidu.com"}]}

The key is wrapped in double quotes, followed by a colon ":", followed by the value of the key

Values can be strings, numbers, arrays, and other data types

Objects are separated by commas

"{}" is used to save objects

"[]" is used to save the array

The dictionaries in json and python look very much alike. What is the difference between them?

1) the key of json can only be a string, and the key of dict can be any hash object, such as string, number, tuple, etc.

2) Dictionary is a data structure, json is a data format; dictionary has many built-in functions and multiple calling methods, while json is a format for data packaging, which is not operable like dictionaries.

3) double quotation marks are mandatory for json strings, and single or double quotation marks can be used for dict strings.

In general, we convert json into a dictionary or list in python, and then manipulate it.

The module of Python dealing with json: json

Pythone3 standard library JSON module, can easily help us to convert and process json data, here mainly refers to serialization (json.dumps (), json.dump ()) and deserialization (json.loads (), json.load ()).

Serialization and deserialization:

The process of converting an object to a data format that can be transmitted over a network or stored on a local disk, such as XML, JSON, or byte strings in a specific format, is called serialization; otherwise, it is called deserialization.

Common JSON module methods:

Json.dumps (): converts objects in Python to string objects in JSON

Json.dump (): converts the python object to a JSON string and outputs it to the fp stream.

Json.loads (): converts string objects in JSON to objects in Python

Json.load (): reads the file that contains the json object.

Everything with s is related to a string, and everything without s is related to a file.

Example:

Convert dictionaries to json strings

Import jsondic = {'name':' xiaoming', 'age': 29} json_str = json.dumps (dic) # returns the json string print (json_str) print (type (json_str)) output: {"name": "xiaoming", "age": 29}

Python Decoding JSON object

Import jsonjson_str ='{"id": "09", "name": "Nitin", "department": "Finance"}'# Convert string to Python dictdict = json.loads (json_str) print (dict) # to access the values in the dictionary, you can use the dictionary's key to access print (dict ['id']) output: {' id':'09, 'name':' Nitin', 'department':' Finance'} 09

Read json file

Import jsonwith open ('test1.json') as f: a = json.load (f) print (a) print (type (a)) output: {' sites': [{'name':' 360, 'url':' www.360.com'}, {'name':' google', 'url':' www.google.com'}, {'name':' baidu', 'url':' www.baidu.com'}]}

Write to json file

Import json dic = {"name": "xiaoming", "age": 20, "phonenumber": "155555555555"} with open ("test2.json", "w") as outfile: json.dump (dic, outfile) file test.json {"name": "xiaoming", "age": 20, "phonenumber": "155555555555"}

Python type conversion correspondence of JSON types

After reading the above, do you have any further understanding of how to deal with the json module in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Object character string data format dictionary sequence module quotation mark file processing name output content type between colon commonly used number array Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi Linux OPPO Reno macOS