0% found this document useful (0 votes)
14 views1 page

code

Uploaded by

luongthedung0505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

code

Uploaded by

luongthedung0505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

import json

class Order:
def __init__(self, json_data):
self.data = json.loads(json_data)
self.order_id = self.data.get("order_id")
self.customer = Customer(self.data.get("customer"))
self.items = [Item(item) for item in self.data.get("items", [])]
class Customer:
def __init__(self, customer_data):
self.name = customer_data.get("name")
self.email = customer_data.get("email")
class Item:
def __init__(self, item_data):
self.product_id = item_data.get("product_id")
self.quantity = item_data.get("quantity")
self.price = item_data.get("price")
# Example usage
json_string = """{
"order_id": 12345,
"customer": {"name": "John Doe", "email": "johndoe@example.com"},
"items": [
{"product_id": 1, "quantity": 2, "price": 19.99},
{"product_id": 2, "quantity": 1, "price": 9.99}
]
}"""
order = Order(json_string)
print("Order ID:", order.order_id)
print("Customer Name:", order.customer.name)
print("Customer Email:", order.customer.email)
for item in order.items:
print(f"Product ID: {item.product_id}, Quantity: {item.quantity}, Price: ${item.price:.2f}")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy