[英]How to read in this JSON file formatted as a dictonary?
So I am working on learning Python and have a project I am working on involving keeping track of vending machines.所以我正在学习 Python 并且有一个我正在从事的项目,涉及跟踪自动售货机。 I was given JSON files to get the information I need, but I don't know how to read it in. My teacher said it is formatted to be read in easily with every vending machine slot as a dictionary but I am very unfamiliar with dictionaries and don't understand how to do it.我得到了 JSON 文件来获取我需要的信息,但我不知道如何读取它。我的老师说它的格式很容易在每个自动售货机插槽中读取为字典,但我对字典非常不熟悉并且不明白该怎么做。 If someone could quickly give me an example of how to read this in I would really appreciate it because I really don't even understand where to start.如果有人能快速给我一个如何阅读这篇文章的例子,我会非常感激,因为我真的不知道从哪里开始。
Here is a section of the JSON file:这是 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
}
]
},
You want the json library.你想要 json 库。
To go from json to dictionary从 json 到字典
x = json.loads(YourDictionary)
to go from dictionary to json从字典到 json
y = json.dumps(YourDictionary)
here's a link reference: https://www.w3schools.com/python/python_json.asp这是一个链接参考: https : //www.w3schools.com/python/python_json.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.