简体   繁体   English

如何将php文件中的JSON数据导入Python

[英]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. 我从一个包含大量数据的网站上获取了一个php文件,但我只需要其中一些。 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. 我想提取JSON格式的数据并在python中使用它。

The file for the example is named JSON1.php . 该示例的文件名为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. 尽管这是我在网上找到的JSON示例。

{
    "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 . 从我可以看到的是标准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. 我能用Google搜索的内容以及在这里找到的内容都无法处理以.php扩展名保存在文件中的JSON数据。

Many thanks! 非常感谢!

You can access json from a .php file the same way as a .json file. 您可以从.php文件访问json,方法与.json文件相同。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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