Python processing json summary

**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 pairs. 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 decodes JSON objects

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

Recommended Posts

Python processing json summary
Python datetime processing time summary
python_ file processing
Python basic summary
Python interview questions summary
Python advanced usage summary
Python high-order function usage summary!
Python on image processing PIL
LeetCode brushing questions summary python3
Python high-order function usage summary!
Python list comprehension operation example summary
Python PIL library image graying processing
A summary of 200 Python standard libraries!
Python processing PDF and CDF examples
Summary of Python calling private attributes
Python decorator simple usage example summary
Python reads and writes json files
Python process and thread summary case analysis
Comprehensive summary of Python built-in exception types
Summary of common operations of Python time module
How does Python handle the json module
Summary of knowledge points about Python unpacking
&quot;Python hands-on learning&quot; Python processing .mat file
Python file and directory operation code summary
Python process and thread summary case analysis