[英]Is there any Gson similar libraries for Python
我是 python 的新手。我正在嘗試為我的 android 應用程序創建 json 響應。 我想知道是否有類似於 GSON 的庫用於 python。
http://nullege.com/codes/search/com.google.gson.Gson在這個鏈接我看到了 Gson 的用法。
誰能告訴我是否有 python 的 GSON 庫或任何其他類似的庫。 另外,如果有的話,請指導我將其集成到代碼中。
你可以使用Python 的 Pykson、JSON 序列化器和反序列化器,<\/a>這有點像 Gson。 它支持對象列表和序列化名稱。
只需將您的對象模型定義為
JsonObject<\/code> ,然后使用
Pykson<\/code>將其來回轉換為 JSON。
class Student(JsonObject):
first_name = StringField(serialized_name="fn")
last_name = StringField(serialized_name="ln")
age = IntegerField(serialized_name="a")
json_text = '{"fn":"John", "ln":"Smith", "a": 25}'
student = Pykson.from_json(json_text, Student)
student_json = Pykson.to_json(student)
assert (json_text == student_json)
Jsonic 是一個輕量級實用程序,用於將 python 對象序列化\/反序列化到 JSON。
例子:
from jsonic import serialize, deserialize
class User(Serializable):
def __init__(self, user_id: str, birth_time: datetime):
super().__init__()
self.user_id = user_id
self.birth_time = birth_time
user = User('id1', datetime(2020,10,11))
obj = serialize(user) # {'user_id': 'id1', 'birth_time': {'datetime': '2020-10-11 00:00:00', '_serialized_type': 'datetime'}, '_serialized_type': 'User'}
new_user : User = deserialize(obj) # new_user is a new instance of user with same attributes
你可以使用 BSON:
https://pymongo.readthedocs.io/en/stable/api/bson/index.html
將 BSON 視為“二進制 JSON”,意思是:
此外,它原生支持:
BSON 是 MongoDb 的“本機”object 編組技術。因此它是一個 object 類型,您可以使用“pymongo”庫訪問它。 您可以加載 pymongo 而不是將其用於 MongoDb,而僅使用 BSON 部分。
我們使用 BSON 來編組長 arrays 的浮點數,JSON 是一個糟糕的選擇。 這對原始 TCP sockets 或其他發送“字節 blob”作為其有效負載的傳輸(例如 RabbitMQ 消息)很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.