简体   繁体   中英

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. 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. 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:

{

    "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.

To go from json to dictionary

x = json.loads(YourDictionary)

to go from dictionary to json

y = json.dumps(YourDictionary)

here's a link reference: https://www.w3schools.com/python/python_json.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM