简体   繁体   中英

How to import JSON data in php file into Python

Please can anyone get me started with the following question?

I get a php file from a website that contains lots of data but I only need some of it. The file is so large I cannot paste it here.

I want to extract the data which is in JSON format and work with it in python.

The file for the example is named JSON1.php . When I open it using a text editor the contents are laid out in this format. Though this is a JSON example I found online.

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

From what I can see it is standard JSON .

What I have been able to google and what I have found here doesn't deal with JSON data saved in a file with .php extention.

Many thanks!

You can access json from a .php file the same way as a .json file.

Example code:

import json

with open("JSON1.php", "rb") as f:
    jsonData = json.loads(f.read())

# Now you can use the json as a python dictionary

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