将一个对象转换为对应的json字符串
import json
class Product:
def __init__(self, name, price, count):
self.name...return {
'name': obj.name,
'price':obj.price,
'count':obj.count
}
jsonStr = json.dumps...将对象列表转换为json数组
f = open('products.json', 'r', encoding='utf-8')
jsonStr = f.read()
f.close()
class Product...__dict__ = d
products = json.loads(jsonStr, object_hook=Product)
print(products)
for product...in products:
print(product.name)
jsonStr = json.dumps(products, default=product2Dict, ensure_ascii