Hướng dẫn can you save a python object to json? - bạn có thể lưu đối tượng python vào json không?

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
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    1. Bàn luận
    2. 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]
    2
    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]
    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]
    4

    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]
    5
    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]
    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]
    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]
    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

    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]
    5
    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]
    3
    {"name": "Dell Alienware", "processor": "Intel Core i7"}
    22218
    {"name": "Dell Alienware", "processor": "Intel Core i7"}
    4

    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]
    5
    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]
    3
    {"name": "Dell Alienware", "processor": "Intel Core i7"}
    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]
    8
    {"name": "Dell Alienware", "processor": "Intel Core i7"}
    9

    class

    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]
    1

    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]
    2
    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]
    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]
    4

    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]
    5
    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]
    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]
    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]
    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]
    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]
    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 import1import2import3import4443import66 ____67

    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]
    0import9
    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 import1json2import3json4import3import66____67

    ____10json9

    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 class1class2import3class4import3class6import7

    ____10class9

    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 class1class2import3Student:4____63Student:6import7

    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]
    0Student: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 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]
    01

    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____103
    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 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]
    05

    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
    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]
    07
    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 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]
    09

    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
    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]
    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]
    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]
    13

    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
    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]
    15
    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]
    16

    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
    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]
    15
    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]
    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]
    15
    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]
    22

    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
    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]
    15
    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]
    25

    Output:

    {"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"}

    Bài Viết Liên Quan

    Chủ Đề