用Python从JSON获取有限的信息

2024-06-28 10:55:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想用Python从JSON中提取特定的位置。我对“data”中的所有条目都感兴趣(见下面的代码)。我感兴趣的条目是“4101”和“3591”。我对更深层次的细节不感兴趣。如何获得条目列表。我的最终结果应该是output=[“4101”,“3591”,等等..]

我正在使用json.dump文件Python。 有什么我可以添加的属性吗json.dump文件(记录)?就像json.dump文件(记录,findonly=“data”,depth=“1”)?你知道吗

records = """{
"status": "ok",
"count": 130,
"data": {
    "4101": {
        "is_gift": false,
        "nation_i18n": "Japan",
        "name": "type-91",
        "level": 1,
        "nation": "japan",
        "is_premium": false,
        "plane_id": 4101,
        "images": {
            "small": "http://worldofwarplanes.eu/static/1.1.0/encyclopedia/planopedia/vehicle/small/type-91.png",
            "large": "http://worldofwarplanes.eu/static/1.1.0/encyclopedia/planopedia/vehicle/large/type-91.png",
            "medium": "http://worldofwarplanes.eu/static/1.1.0/encyclopedia/planopedia/vehicle/medium/type-91.png"
        },
        "name_i18n": "Nakajima Type-91",
        "type": "fighter"
    },
    "3591": {......[it goes on and on]

Tags: 文件jsonhttpdatapngtypestatic条目
2条回答

因为我搜索的所有条目都是4位数字,所以我选择了regexp:

result = re.findall('\d{4}', record)
print result

使用^{}将JSON解析为Python数据结构,然后访问所需的任何内容:

json_file = open('data.json')
dct = json.load(json_file)
print dct['data'].keys()

输出:

[u'4101', u'3591']

相关问题 更多 >