【问题标题】:Need help extracting information out of a JSON file in python需要帮助从 python 中的 JSON 文件中提取信息
【发布时间】:2020-11-30 05:43:03
【问题描述】:

我需要在 python 上从这个 JSON 文件中提取密钥,我该怎么做?

{
    "expand": "schema,names",
    "issues": [
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "10012",
            "key": "SM-2",
            "self": "https://isml.atlassian.net/rest/api/3/issue/10012"
        },
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "10013",
            "key": "SM-3",
            "self": "https://isml.atlassian.net/rest/api/3/issue/10013"
        },
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "10014",
            "key": "SM-4",
            "self": "https://isml.atlassian.net/rest/api/3/issue/10014"
        },
    ],
    "maxResults": 50,
    "startAt": 0,
    "total": 3
}

例如我想创建一个类似 ["SM-2", "SM-3", "SM-4"] 的列表

提前致谢!

【问题讨论】:

  • 你尝试过什么,它到底有什么问题?你看过json 模块吗?解析JSON?你知道如何处理列表和字典吗?
  • 使用标准库中的json模块...网上有很多这样的例子
  • 如果你只有那个数据结构,你会怎么做?
  • 我以前从未使用过 json,所以需要一些帮助...在网上找不到这样的例子,但如果有我的坏处!

标签: python json extract


【解决方案1】:

JSON 格式错误 对于这个 JSON

[
    {
    "expand": "schema,names",
    "issues": [
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "10012",
            "key": "SM-2",
            "self": "https://isml.atlassian.net/rest/api/3/issue/10012"
        },
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "10013",
            "key": "SM-3",
            "self": "https://isml.atlassian.net/rest/api/3/issue/10013"
        },
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "10014",
            "key": "SM-4",
            "self": "https://isml.atlassian.net/rest/api/3/issue/10014"
        }
    ],
    "maxResults": 50,
    "startAt": 0,
    "total": 3
    }
]

试试这个代码

import json
j = json.load(open('Json.json', 'r'))

for d in j[0]['issues']:
    print(d['key'])

【讨论】:

    【解决方案2】:

    你发布的json不好,希望是一个例子..
    JSON 不接受“,”多余的逗号

    {
        "expand": "schema,names",
        "issues": [
            {
                "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
                "id": "10012",
                "key": "SM-2",
                "self": "https://isml.atlassian.net/rest/api/3/issue/10012"
            },
            {
                "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
                "id": "10013",
                "key": "SM-3",
                "self": "https://isml.atlassian.net/rest/api/3/issue/10013"
            },
            {
                "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
                "id": "10014",
                "key": "SM-4",
                "self": "https://isml.atlassian.net/rest/api/3/issue/10014"
            },    <<<<<<  THIS COMMA is not acceptable in a valid json !!!!
        ],
        "maxResults": 50,
        "startAt": 0,
        "total": 3
    }
    

    使用 ipython

    In [8]: import json
           ...:
           ...: r = '{\
           ...:     "expand": "schema,names",\
           ...:     "issues": [\
           ...:         {\
           ...:             "expand": 
     "operations,versionedRepresentations,editmeta,changelog,renderedFields",\
           ...:             "id": "10012",\
           ...:             "key": "SM-2",\
           ...:             "self": "https://isml.atlassian.net/rest/api/3/issue/10012"\
           ...:         },\
           ...:         {\
           ...:             "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",\
           ...:             "id": "10013",\
           ...:             "key": "SM-3",\
           ...:             "self": "https://isml.atlassian.net/rest/api/3/issue/10013"\
           ...:         },\
           ...:         {\
           ...:             "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",\
           ...:             "id": "10014",\
           ...:             "key": "SM-4",\
           ...:             "self": "https://isml.atlassian.net/rest/api/3/issue/10014"\
           ...:         }\
           ...:     ],\
           ...:     "maxResults": 50,\
           ...:     "startAt": 0,\
           ...:     "total": 3\
           ...: }'
           ...:
    
        In [9]: j = json.loads(r)
    
        In [10]: j['issues']
        Out[10]:
        [{'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
          'id': '10012',
          'key': 'SM-2',
          'self': 'https://isml.atlassian.net/rest/api/3/issue/10012'},
         {'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
          'id': '10013',
          'key': 'SM-3',
          'self': 'https://isml.atlassian.net/rest/api/3/issue/10013'},
         {'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
          'id': '10014',
          'key': 'SM-4',
          'self': 'https://isml.atlassian.net/rest/api/3/issue/10014'}]
    
        In [11]: l = j['issues']
    
        In [12]: for issue in l:
            ...:     print(l)
            ...:
        [{'expand': 
         'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '10012', 'key': 'SM-2', 'self': 
        'https://isml.atlassian.net/rest/api/3/issue/10012'}, {'expand': 
        'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': 
        '10013', 'key': 'SM-3', 'self': 
        'https://isml.atlassian.net/rest/api/3/issue/10013'}, {'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '10014', 'key': 'SM-4', 'self': 'https://isml.atlassian.net/rest/api/3/issue/10014'}]
        [{'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '10012', 'key': 'SM-2', 'self': 'https://isml.atlassian.net/rest/api/3/issue/10012'}, {'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '10013', 'key': 'SM-3', 'self': 'https://isml.atlassian.net/rest/api/3/issue/10013'}, {'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '10014', 'key': 'SM-4', 'self': 'https://isml.atlassian.net/rest/api/3/issue/10014'}]
        [{'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '10012', 'key': 'SM-2', 'self': 'https://isml.atlassian.net/rest/api/3/issue/10012'}, {'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '10013', 'key': 'SM-3', 'self': 'https://isml.atlassian.net/rest/api/3/issue/10013'}, {'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '10014', 'key': 'SM-4', 'self': 'https://isml.atlassian.net/rest/api/3/issue/10014'}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      相关资源
      最近更新 更多