To put JSON The string resolves to a Python object , You can use Python The built-in json library .json In the bag loads() Function to parse JSON strand .
import json
pythonObj = json.loads(jsonStr)
among ,jsonStr Is a containing JSON String of data , and json.loads() Will return a Python object . be based on JSON String data structure , return Python The data type of the object will be list or dictionary .
Next, we create an include JSON String of data , And then use json.loads() Function to parse it .
# Parse JSON String to Python Dictionary
import json
jsonstr = '{"name":"Tesla", "age":2, "city":"New York"}'
pythonOjb = json.loads(jsonstr)
print(type(pythonOjb))
print(pythonOjb)
name = pythonOjb["name"]
print(name)
Execution and output :
In this example JSON A string is a single element that contains multiple key value pairs . therefore loads() The function should JSON The data type obtained from string parsing is Dictionary .
Next let's look at another set of elements with multiple key value pairs JSON character string , We use the same json.loads() Function to parse it .
# Parse JSON String to Python List
import json
jsonStr = '[{"name":"Tesla", "age":2, "city":"New York"}, {"name":"Jim", "age":3, "city":"Boston"}]'
pythonObj = json.loads(jsonStr)
print(type(pythonObj))
print(pythonObj)
print(type(pythonObj[0]))
print(pythonObj[0])
city = pythonObj[1]["city"]
print(city)
Execution and output :
In this example JSON String contains more than one set of elements . Therefore, from loads() The data type returned by function parsing is a list , The data type of the elements in the list is Dictionary .
You have to put one Python Class object to JSON character string , Or save the properties of the class object as JSON character string , Use json.dumps() Method .
jsonStr = json.dumps(myobject.__dict__)
among ,
In the next example , We define a Python class , Then I create an object of this class , Finally, the attribute is transformed into a JSON character string .
# Convert Python Class Object to JSON string
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
laptop1 = Laptop()
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
jsonStr = json.dumps(laptop1.__dict__)
print(jsonStr)
Execution and output :
You can see , Attribute names have been converted to JSON And the property value is converted to JSON Value .
In the following example , We defined a string with different data types 、 Of integer floating-point properties Python class , Then I create an object of this class , Then convert the properties of the objects of this class to JSON strand .
# Convert Properties of Python Class Object to JSON string
import json
class Laptop:
def __init__(self, name, processor, hdd, ram, cost):
self.name = name
self.processor = processor
self.hdd = hdd
self.ram = ram
self.cost = cost
laptop1 = Laptop('Dell Alienware', 'Intel Core i7', 512, 8, 2500.00)
jsonStr = json.dumps(laptop1.__dict__)
print(jsonStr)
Execution and output :
To put Python Tuple to JSON Array , Pass a tuple as an argument to a method json.dumps() Method .
jsonStr = json.dumps(mytuple)
In the next example , We create a tuple and convert it to a JSON character string .
# Convert Python Tuple to JSON String
import json
mytupe = ("python", "json", "mysql")
jsonStr = json.dumps(mytupe)
print(jsonStr)
Execution and output :
Next, we create a tuple of elements of different data types , And then convert it to a JSON character string . after , We should JSON String to parse and access its elements .
# Convert Python Tuple with Different Datatypes to JSON String
import json
mytupe = ("python", "json", 22, 23.04)
jsonStr = json.dumps(mytupe)
print(type(jsonStr))
print(jsonStr)
jsonArr = json.loads(jsonStr)
print(type(jsonArr))
print(jsonArr[2])
Execution and output :