Article From:https://www.cnblogs.com/tsembrace/p/9033584.html

JSON: JavaScript Object Notation(JavaScriptObject markup)

Its essence is “a lightweight data interchange format”. JSON string is the expression form of JSON.

A string that conforms to the JSON format is called a JSON string.

JSONAdvantages (XML): easy to read, easy to analyze, and efficient in network transmission. It’s very suitable for it.Exchange data across languages.

For Python, the connotations of the JSON format strings they receive are in a variety of forms, such as “dictionaries” and “dictionaries as a list of elements,” although it is always shown as a string.

For example:

#JSON objectString form
json_str = '{"name": "qiyue", "age": 18}'
#JSON arrayString form
json_str0 = '[{"name": "qiyue", "age": 18, "flag":false}, {"name": "bayue", "age": 19}]'

It is important to note that the JSON format must be represented by double quotes.

One, JSON’s anti serialization in Python

If the JSON information is received in Python, the JSON module is provided inside, which can “reverse the serialization” of the information string into the corresponding format variable in the Python language through the json.loads () method.

 

import json
#JSON objectString form
json_str = '{"name": "qiyue", "age": 18}'
student = json.loads(json_str)
print(type(student))
print(student)
#Output:
'''
<class 'dict'>
{'name': 'qiyue', 'age': 18}
'''
import json
#JSON arrayString form
json_str0 = '[{"name": "qiyue", "age": 18, "flag":false}, {"name": "bayue", "age": 19}]'
student0 = json.loads(json_str0)
print(type(student0))
print(student0)
print(student0[0]['name'])
print(student0[1]['age'])
#Output:
'''
<class 'list'>
[{'name': 'qiyue', 'age': 18, 'flag': False}, {'name': 'bayue', 'age': 19}]
qiyue
19
'''

Serialization of two, Python to JSON formats

Serialization is the conversion of data in Python into data in JSON format, through the dumps () method in the JSON module.

import json

a0 = 10
a1 = 3.1415926
a2 = 'Hello'
a3 = [1,3,5,7,9]
a4 = ('x', 'y', 'z')
a5 = {1: 11, 2: 22, 3:33}

s0 = json.dumps(a0)
s1 = json.dumps(a1)
s2 = json.dumps(a2)
s3 = json.dumps(a3)
s4 = json.dumps(a4)
s5 = json.dumps(a5)

print(s0)
print(s1)
print(s2)
print(s3)
print(s4)
print(s5)

'''
Output:TenThree point one four one five nine two six"Hello"[1, 3, 5, 7, 9]["X", "Y", "Z"]{"1": 11, "2": 22, "3": 33}'''
import json

student = [
           {'name': 'qiyue', 'age': 18, 'flag': False},
           {'name': 'bayue', 'age': 19}
           ]

json_str = json.dumps(student)
print(type(json_str))
print(json_str)
'''
Output:< class'str'>[{"name": "Qiyue", "age": 18, "flag": false}, {"name": "Bayue", "age": 19}]).'''

Three, JSON, JSON objects, and JSON strings

JSONIt is just a data standard format for data transmission and exchange, and it is a realization of one aspect of ECMASCRIPT standard.

JSONThe object is only in the JavaScript language, and there is no JSON object for other languages such as Python, because for Python, only the JSON string interacts with it (serialization and anti serialization).)

 

Leave a Reply

Your email address will not be published. Required fields are marked *