Làm thế nào để bạn giải tuần tự hóa json thành một đối tượng trong python?

Tìm hiểu cách giải tuần tự hóa chuỗi JSON và chuyển đổi dữ liệu JSON đã giải tuần tự hóa thành một lớp tùy chỉnh [e. g.

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
2] bằng cách mở rộng phương pháp
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
3 hoặc
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
4

Trong ví dụ giải tuần tự hóa tùy chỉnh Python, chúng ta có lớp

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
2 sau. Ở đây,
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
1 thuộc loại datetime và chúng ta có thể định nghĩa bất kỳ chuỗi định dạng nào cho nó

import datetime

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

1. Phương thức Deserializer tùy chỉnh Python sử dụng object_hook

1. 1. Thêm Phương thức Deserializer vào Lớp

Theo mặc định, các phương thức

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
2 hoặc
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
3 đọc JSON và trả về dưới dạng đối tượng từ điển python. Trong phương thức deserializer, chúng ta phải chuyển đổi
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
4 thành lớp tùy chỉnh của mình

Phương thức

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
5 đã cho nhận một đối số phương thức từ điển và tạo một thể hiện
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
2 mới bằng cách gọi hàm tạo của nó

Lưu ý cách chúng tôi đang sử dụng các thuộc tính

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
7 và
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
8 để xác thực rằng chúng tôi đang giải mã JSON để sửa lớp trong đúng mô-đun. Trong trường hợp này, thông tin về lớp và mô-đun là một phần của chính dữ liệu JSON

Chúng tôi có thể tự do tùy chỉnh logic khử lưu huỳnh khi chúng tôi cần trong ứng dụng

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst

1. 2. Sử dụng thuộc tính ‘object_hook’ trên json. tải []

Tiếp theo, để sử dụng phương thức deserializer này, hãy sử dụng thuộc tính object_hook trong phương thức

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
9. Nó sẽ được tự động gọi bởi python trong khi chuyển đổi JSON sang đối tượng phức tạp

import datetime

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate
0

đầu ra chương trình

import datetime

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate
1

2. Giải tuần tự hóa tùy chỉnh Python bằng JSONDecoder

Một cách khác, để thêm logic khử lưu huỳnh tùy chỉnh, là mở rộng lớp

import datetime

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate
00

Lớp

import datetime

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate
01 sau đây khai báo phương thức
import datetime

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate
02 cung cấp logic để chuyển đổi JSON thành lớp phức tạp

Phương thức

import datetime

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate
02 kiểm tra tất cả các đối tượng từ điển được đọc bởi phương thức
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
9 và kiểm tra các thuộc tính
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
7 và
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
8 trong từ điển

Nếu nó tìm thấy thông tin về lớp và mô-đun, phương thức

import datetime

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate
02 sẽ nhập và tải lớp. Sau đó, nó tạo một thể hiện mới của lớp bằng cách gọi hàm tạo của nó và chuyển các cặp khóa-giá trị từ điển làm đối số hàm tạo

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
0

1. 2. Sử dụng thuộc tính 'cls' trên json. bãi []

Để sử dụng

class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
3 tùy chỉnh, hãy sử dụng thuộc tính cls trong phương pháp
class User:

  def __init__[self, id, name, birthdate]:
    self.id = id
    self.name = name
    if isinstance[birthdate, str]:
      self.birthdate = datetime.datetime.strptime[birthdate, "%d %b %y"]
    else:
      self.birthdate = birthdate

  def to_object[d]:
    if d['__class__'] == 'User' and d['__module__'] == 'user':
      inst = User[d['id'], d['name'], d['birthdate']]
    else:
      inst = d
    return inst
9. Nó sẽ được tự động gọi bởi python trong khi chuyển đổi JSON thành đối tượng phức tạp

Làm cách nào để chuyển đổi JSON thành đối tượng trong Python?

Sử dụng namedtuple và object_hook để chuyển đổi dữ liệu JSON thành đối tượng Python tùy chỉnh. Chúng ta có thể sử dụng tham số object_hook của json. tải[] và json. phương thức tải [].

Làm cách nào để chuyển đổi chuỗi JSON thành đối tượng lớp trong Python?

Sử dụng json. hàm tải[] . json. hàm loading[] chấp nhận đầu vào là một chuỗi hợp lệ và chuyển đổi nó thành một từ điển Python. Quá trình này được gọi là deserialization – hành động chuyển đổi một chuỗi thành một đối tượng.

Làm cách nào để tuần tự hóa và giải tuần tự hóa JSON bằng Python?

Làm việc với dữ liệu JSON trong Python . dump[] sẽ ghi dữ liệu Python vào một đối tượng dạng tệp . Chúng tôi sử dụng điều này khi chúng tôi muốn tuần tự hóa dữ liệu Python của mình thành tệp JSON bên ngoài. dumps[] sẽ ghi dữ liệu Python vào một chuỗi ở định dạng JSON.

Chủ Đề