How does Python handle the json module

**First of all, understand what is JSON? **

JSON: JavaScript Object Notation [JavaScript Object Notation]

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

The basic syntax of JSON is as follows: JSON name/value pair. The writing format of JSON data is: name/value pairs. 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 the object: key can only be string, value can be object, array, string, number, true/false, null

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

**json looks a lot like a dictionary in python. What is the difference between the two? **

1 ) The key of json can only be a string, and the key of dict can be any hashable object, for example: string, number, tuple, etc.;

2 ) A dictionary is a data structure, and json is a data format; a dictionary has many built-in functions and a variety of calling methods, and json is a format for data packaging, which is not operable like a dictionary;

3 ) Double quotes are mandatory for json strings, single quotes and double quotes for dict strings;

Generally speaking, we will convert json into a dictionary or list in python, and then operate on it.

Python module for processing json: json

The standard library JSON module of Pythone3 can help us easily 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 into a data format (such as XML, JSON, or a byte string in a specific format) that can be transmitted over the network or stored on a local disk is called serialization; otherwise, it is called deserialization.

Commonly used JSON module methods:

Those with s are related to strings, and those without s are related to files.

**Example: **

Convert dictionary to json string

import json
dic ={'name':'xiaoming','age':29}
json_str = json.dumps(dic)#Return json string
print(json_str)print(type(json_str))
Output:
{" name":"xiaoming","age":29}<class'str'

Python decode JSON object

import json
json_str ='{"id":"09", "name": "Nitin", "department":"Finance"}'
# Convert string to Python dict
dict = json.loads(json_str)print(dict)
# After being converted into a dictionary, to access the value in it, you can use the key of the dictionary to access
print(dict['id'])
Output:
{' id':'09','name':'Nitin','department':'Finance'}09

Read json file

import json
withopen('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'}]}<class'dict'

Write json file

import json
 dic ={"name":"xiaoming","age":20,"phonenumber":"15555555555"}withopen("test2.json","w")as outfile:
 json.dump(dic, outfile)

 File test.json {"name":"xiaoming","age":20,"phonenumber":"15555555555"}

Python type conversion JSON type correspondence

JSON type conversion to Python type comparison table

The above is the details of how Python handles the json module. For more information on how Python handles the json module, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

How does Python handle the json module
How does python handle the program cannot be opened
How does python judge the module installation is complete
How to view the python module
How does python improve the calculation speed
How to learn the Python time module
How to install the downloaded module in python
How does Python store data to json files
How does Python output integers
How does python call the key of a dictionary
How does python get input examples from the keyboard
How does python update packages
How does python perform matrix operations
How does Python list update value
How to save the python program
How does python call java classes
How does python import dependency packages
How does python enter interactive mode
How does Python generate xml files
How Python implements the timer function
How does python determine prime numbers
Python3 module
How does python call its own function
How does python distinguish return and yield
How does Python call cmd using OS modules
How to use the round function in python
How to use the zip function in Python
What does the tab key in python mean
How to use the format function in python
Detailed explanation of the usage of Python decimal module
How to enter python through the command line
How about learning python at the age of 27?
2.1 The Python Interpreter (python interpreter)
How to switch the hosts file using python
Python how to move the code collectively right
Python Lesson 37-Module
Python is short-what does the spirit cage say
How to add the gzip module to Nginx on Ubuntu 14.04
How to practice after the python file is written
How to understand the introduction of packages in Python
Python uses the re module to verify dangerous characters
Python3 built-in module usage
Python processing json summary
Python3 external module use
Consolidate the Python foundation (2)
Prettytable module of python
How python was invented
How Python parses XML
How long does it take to learn python by myself?
How to find the area of a circle in python