因此,我正在学习Python,并且正在从事一个涉及跟踪自动售货机的项目。我得到了JSON文件来获取所需的信息,但是我不知道该如何读入。我的老师说,该文件的格式易于在每个自动售货机插槽中作为字典读取,但我对字典非常不熟悉。而且不知道该怎么做。如果有人可以快速给我提供一个如何阅读本示例的示例,我将不胜感激,因为我什至都不知道从哪里开始。
这是JSON文件的一部分:
{
"contents": [
{
"row": "A",
"slots": [
{
"current_stock": 2,
"item_name": "Coke",
"item_price": 1.75,
"last_stock": 3,
"slot_number": 1
},
{
"current_stock": 0,
"item_name": "Coke",
"item_price": 1.75,
"last_stock": 6,
"slot_number": 2
},
{
"current_stock": 1,
"item_name": "Coke",
"item_price": 1.75,
"last_stock": 2,
"slot_number": 3
},
{
"current_stock": 3,
"item_name": "Coke",
"item_price": 1.75,
"last_stock": 3,
"slot_number": 4
},
{
"current_stock": 2,
"item_name": "Coke",
"item_price": 1.75,
"last_stock": 2,
"slot_number": 5
},
{
"current_stock": 0,
"item_name": "Coke",
"item_price": 1.75,
"last_stock": 5,
"slot_number": 6
},
{
"current_stock": 6,
"item_name": "Coke Zero",
"item_price": 1.75,
"last_stock": 8,
"slot_number": 7
},
{
"current_stock": 2,
"item_name": "Coke Zero",
"item_price": 1.75,
"last_stock": 4,
"slot_number": 8
},
{
"current_stock": 4,
"item_name": "Coke Zero",
"item_price": 1.75,
"last_stock": 7,
"slot_number": 9
}
]
},
答案 0 :(得分:0)
您想要json库。
要从json转到字典
x = json.loads(YourDictionary)
从字典转到json
y = json.dumps(YourDictionary)