Nội dung
- Giới thiệu
- Cú pháp - json.dumps []
- Ví dụ 1: Chuyển đổi đối tượng lớp Python thành Chuỗi JSON
- Ví dụ 2: Chuyển đổi thuộc tính của đối tượng lớp Python sang chuỗi JSON
- Bản tóm tắt
Để chuyển đổi đối tượng lớp Python thành chuỗi JSON hoặc lưu các tham số của đối tượng lớp thành chuỗi JSON, hãy sử dụng phương thức json.dumps [].
Trong hướng dẫn này, chúng tôi sẽ học cách xây dựng một chuỗi JSON từ một đối tượng lớp Python.
Cú pháp - json.dumps []
Ví dụ 1: Chuyển đổi đối tượng lớp Python thành Chuỗi JSON
jsonStr = json.dumps[myobject.__dict__]
Ví dụ 2: Chuyển đổi thuộc tính của đối tượng lớp Python sang chuỗi JSON
- Bản tóm tắt is the module.
- Để chuyển đổi đối tượng lớp Python thành chuỗi JSON hoặc lưu các tham số của đối tượng lớp thành chuỗi JSON, hãy sử dụng phương thức json.dumps []. is the method that converts the python object to JSON string. It returns a JSON string.
- Trong hướng dẫn này, chúng tôi sẽ học cách xây dựng một chuỗi JSON từ một đối tượng lớp Python. is the Python Class object and myobject.__dict__ gets the dictionary version of object parameters.
Ví dụ 1: Chuyển đổi đối tượng lớp Python thành Chuỗi JSON
Ví dụ 2: Chuyển đổi thuộc tính của đối tượng lớp Python sang chuỗi JSON
Bản tóm tắt
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
Để chuyển đổi đối tượng lớp Python thành chuỗi JSON hoặc lưu các tham số của đối tượng lớp thành chuỗi JSON, hãy sử dụng phương thức json.dumps [].
Trong hướng dẫn này, chúng tôi sẽ học cách xây dựng một chuỗi JSON từ một đối tượng lớp Python.
{"name": "Dell Alienware", "processor": "Intel Core i7"}
Sau đây là cú pháp của hàm json.dumps [].
Ví dụ 2: Chuyển đổi thuộc tính của đối tượng lớp Python sang chuỗi JSON
Bản tóm tắt
Bản tóm tắt
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
#create object
laptop1 = Laptop['Dell Alienware', 'Intel Core i7', 512, 8, 2500.00]
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
Để chuyển đổi đối tượng lớp Python thành chuỗi JSON hoặc lưu các tham số của đối tượng lớp thành chuỗi JSON, hãy sử dụng phương thức json.dumps [].
Trong hướng dẫn này, chúng tôi sẽ học cách xây dựng một chuỗi JSON từ một đối tượng lớp Python.
{"name": "Dell Alienware", "processor": "Intel Core i7", "hdd": 512, "ram": 8, "cost": 2500.0}
Bản tóm tắt
Để chuyển đổi đối tượng lớp Python thành chuỗi JSON hoặc lưu các tham số của đối tượng lớp thành chuỗi JSON, hãy sử dụng phương thức json.dumps [].
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận
- Chuyển đổi đối tượng lớp sang JSON được thực hiện bằng gói JSON trong Python. json.dumps [] chuyển đổi đối tượng Python thành chuỗi JSON. Mỗi đối tượng Python có một thuộc tính được ký hiệu là __dict__ và điều này lưu trữ các thuộc tính của đối tượng. & NBSP;
Đối tượng trước tiên được chuyển đổi thành định dạng từ điển bằng thuộc tính __dict__.json.dumps[Object obj]
Từ điển mới được tạo này được truyền dưới dạng tham số cho json.dumps [] sau đó mang lại một chuỗi JSON.Expects a dictionary object.
Cú pháp: json.dumps [đối tượng obj]json string
Tham số: Mong đợi một đối tượng từ điển.
Python3
Trả về: Chuỗi JSON
Theo mã Python chuyển đổi một đối tượng học sinh lớp Python thành JSON.
import
json
class
Student:
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0____11 import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
2import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
3import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
4import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
5import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
3import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
7import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
9import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
5import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
3{"name": "Dell Alienware", "processor": "Intel Core i7"}
22218 {"name": "Dell Alienware", "processor": "Intel Core i7"}
4import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
5import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
3{"name": "Dell Alienware", "processor": "Intel Core i7"}
7import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 {"name": "Dell Alienware", "processor": "Intel Core i7"}
9class
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
#create object
laptop1 = Laptop['Dell Alienware', 'Intel Core i7', 512, 8, 2500.00]
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
1import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0____11 import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
2import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
3import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
4import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
5import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
3import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
7import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
9{"roll_no": "85", "name": "Swapnil", "batch": "IMT"} {"roll_no": "124", "name": "Akash", "batch": "IMT"} {"brand": "Honda", "name": "city", "batch": "2005"} {"brand": "Honda", "name": "Amaze", "batch": "2011"}2
{"roll_no": "85", "name": "Swapnil", "batch": "IMT"} {"roll_no": "124", "name": "Akash", "batch": "IMT"} {"brand": "Honda", "name": "city", "batch": "2005"} {"brand": "Honda", "name": "Amaze", "batch": "2011"}3
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 {"roll_no": "85", "name": "Swapnil", "batch": "IMT"} {"roll_no": "124", "name": "Akash", "batch": "IMT"} {"brand": "Honda", "name": "city", "batch": "2005"} {"brand": "Honda", "name": "Amaze", "batch": "2011"}6
{"roll_no": "85", "name": "Swapnil", "batch": "IMT"} {"roll_no": "124", "name": "Akash", "batch": "IMT"} {"brand": "Honda", "name": "city", "batch": "2005"} {"brand": "Honda", "name": "Amaze", "batch": "2011"}7
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0{"roll_no": "85", "name": "Swapnil", "batch": "IMT"} {"roll_no": "124", "name": "Akash", "batch": "IMT"} {"brand": "Honda", "name": "city", "batch": "2005"} {"brand": "Honda", "name": "Amaze", "batch": "2011"}9
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 import
1import
2import
3import
4443import
66 ____67import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0import
9import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 import
1json
2import
3json
4import
3import
66____67____10json
9
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 class
1class
2import
3class
4import
3class
6import
7____10class
9
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 class
1class
2import
3Student:
4____63Student:
6import
7import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0Student:
9import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
01import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0____103import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
05import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
07import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
09import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
11import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
8 import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
13import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
15import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
16import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
15import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
19____10
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
15import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
22import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
0import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
15import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop[]
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps[laptop1.__dict__]
#print json string
print[jsonStr]
25Output:
{"roll_no": "85", "name": "Swapnil", "batch": "IMT"} {"roll_no": "124", "name": "Akash", "batch": "IMT"} {"brand": "Honda", "name": "city", "batch": "2005"} {"brand": "Honda", "name": "Amaze", "batch": "2011"}