【问题标题】:How to access JSON object within JSON array python?如何在 JSON 数组 python 中访问 JSON 对象?
【发布时间】:2020-07-02 17:34:16
【问题描述】:

我正在使用 Reddit API 的包装器来返回有关 cmets 的信息。返回信息的方式让我非常困惑,我无法获得所需的信息。

所以 API 以这种格式返回信息:

comment(all_awardings=[], associated_award=None, author='raidoctober', author_flair_background_color=None, author_flair_css_class=None, author_flair_richtext=[], author_flair_template_id=None, author_flair_text=None, author_flair_text_color=None, author_flair_type='text', author_fullname='t2_1ekqex92', author_patreon_flair=False, author_premium=False, awarders=[], body="Haha, yeah I thought about it. But it's probably not worth it cause of all the taxes, copart fees, cost of turning a Salvage title into a rebuilt and the insurance deductible.", collapsed_because_crowd_control=None, created_utc=1591296781, gildings={}, id='fsw0scp', is_submitter=True, link_id='t3_gwn3rw', locked=False, no_follow=True, parent_id='t1_fsvyhq1', permalink='/r/motorcycles/comments/gwn3rw/did_copart_steal_my_motorcycle/fsw0scp/', retrieved_on=1591301318, score=1, send_replies=True, stickied=False, subreddit='motorcycles', subreddit_id='t5_2qi6d', top_awarded_type=None, total_awards_received=0, treatment_tags=[], created=1591321981.0, d_={'all_awardings': [], 'associated_award': None, 'author': 'raidoctober', 'author_flair_background_color': None, 'author_flair_css_class': None, 'author_flair_richtext': [], 'author_flair_template_id': None, 'author_flair_text': None, 'author_flair_text_color': None, 'author_flair_type': 'text', 'author_fullname': 't2_1ekqex92', 'author_patreon_flair': False, 'author_premium': False, 'awarders': [], 'body': "Haha, yeah I thought about it. But it's probably not worth it cause of all the taxes, copart fees, cost of turning a Salvage title into a rebuilt and the insurance deductible.", 'collapsed_because_crowd_control': None, 'created_utc': 1591296781, 'gildings': {}, 'id': 'fsw0scp', 'is_submitter': True, 'link_id': 't3_gwn3rw', 'locked': False, 'no_follow': True, 'parent_id': 't1_fsvyhq1', 'permalink': '/r/motorcycles/comments/gwn3rw/did_copart_steal_my_motorcycle/fsw0scp/', 'retrieved_on': 1591301318, 'score': 1, 'send_replies': True, 'stickied': False, 'subreddit': 'motorcycles', 'subreddit_id': 't5_2qi6d', 'top_awarded_type': None, 'total_awards_received': 0, 'treatment_tags': [], 'created': 1591321981.0})

我尝试使用

转换为 JSON
x = json.dumps(hit, sort_keys=True, indent=4)

# hit is the information returned (it is the comment before conversion)

将评论转换成这种 JSON 格式:

[
    [],
    null,
    "raidoctober",
    null,
    null,
    [],
    null,
    null,
    null,
    "text",
    "t2_1ekqex92",
    false,
    false,
    [],
    "Haha, yeah I thought about it. But it's probably not worth it cause of all the taxes, copart fees, cost of turning a Salvage title into a rebuilt and the insurance deductible.",
    null,
    1591296781,
    {},
    "fsw0scp",
    true,
    "t3_gwn3rw",
    false,
    true,
    "t1_fsvyhq1",
    "/r/motorcycles/comments/gwn3rw/did_copart_steal_my_motorcycle/fsw0scp/",
    1591301318,
    1,
    true,
    false,
    "motorcycles",
    "t5_2qi6d",
    null,
    0,
    [],
    1591321981.0,
    {
        "all_awardings": [],
        "associated_award": null,
        "author": "raidoctober",
        "author_flair_background_color": null,
        "author_flair_css_class": null,
        "author_flair_richtext": [],
        "author_flair_template_id": null,
        "author_flair_text": null,
        "author_flair_text_color": null,
        "author_flair_type": "text",
        "author_fullname": "t2_1ekqex92",
        "author_patreon_flair": false,
        "author_premium": false,
        "awarders": [],
        "body": "Haha, yeah I thought about it. But it's probably not worth it cause of all the taxes, copart fees, cost of turning a Salvage title into a rebuilt and the insurance deductible.",
        "collapsed_because_crowd_control": null,
        "created": 1591321981.0,
        "created_utc": 1591296781,
        "gildings": {},
        "id": "fsw0scp",
        "is_submitter": true,
        "link_id": "t3_gwn3rw",
        "locked": false,
        "no_follow": true,
        "parent_id": "t1_fsvyhq1",
        "permalink": "/r/motorcycles/comments/gwn3rw/did_copart_steal_my_motorcycle/fsw0scp/",
        "retrieved_on": 1591301318,
        "score": 1,
        "send_replies": true,
        "stickied": false,
        "subreddit": "motorcycles",
        "subreddit_id": "t5_2qi6d",
        "top_awarded_type": null,
        "total_awards_received": 0,
        "treatment_tags": []
    }
]

我尝试通过索引来访问它,但有时数组的大小不同,因此结果不准确。

我需要“作者”、“正文”和“永久链接”标签。

如果这太含糊,我很抱歉!如果您需要更多信息/说明,请告诉我。

【问题讨论】:

  • 为什么要将某些东西转换为 JSON 字符串以访问其中的元素?
  • 我认为这是访问它的最简单方法。我也不确定是否可以在不将其转换为 JSON 的情况下访问键/值对。

标签: python arrays json python-3.x


【解决方案1】:

这有帮助吗?

hit = [
    [],
    None,
    "raidoctober",
    None,
    None,
    [],
    None,
    None,
    None,
    "text",
    "t2_1ekqex92",
    False,
    False,
    [],
    "Haha, yeah I thought about it. But it's probably not worth it cause of all the taxes, copart fees, cost of turning a Salvage title into a rebuilt and the insurance deductible.",
    None,
    1591296781,
    {},
    "fsw0scp",
    True,
    "t3_gwn3rw",
    False,
    True,
    "t1_fsvyhq1",
    "/r/motorcycles/comments/gwn3rw/did_copart_steal_my_motorcycle/fsw0scp/",
    1591301318,
    1,
    True,
    False,
    "motorcycles",
    "t5_2qi6d",
    None,
    0,
    [],
    1591321981.0,
    {
        "all_awardings": [],
        "associated_award": None,
        "author": "raidoctober",
        "author_flair_background_color": None,
        "author_flair_css_class": None,
        "author_flair_richtext": [],
        "author_flair_template_id": None,
        "author_flair_text": None,
        "author_flair_text_color": None,
        "author_flair_type": "text",
        "author_fullname": "t2_1ekqex92",
        "author_patreon_flair": False,
        "author_premium": False,
        "awarders": [],
        "body": "Haha, yeah I thought about it. But it's probably not worth it cause of all the taxes, copart fees, cost of turning a Salvage title into a rebuilt and the insurance deductible.",
        "collapsed_because_crowd_control": None,
        "created": 1591321981.0,
        "created_utc": 1591296781,
        "gildings": {},
        "id": "fsw0scp",
        "is_submitter": True,
        "link_id": "t3_gwn3rw",
        "locked": False,
        "no_follow": True,
        "parent_id": "t1_fsvyhq1",
        "permalink": "/r/motorcycles/comments/gwn3rw/did_copart_steal_my_motorcycle/fsw0scp/",
        "retrieved_on": 1591301318,
        "score": 1,
        "send_replies": True,
        "stickied": False,
        "subreddit": "motorcycles",
        "subreddit_id": "t5_2qi6d",
        "top_awarded_type": None,
        "total_awards_received": 0,
        "treatment_tags": []
    }
]

for item in hit:
    if type(item) is dict:
        if "author" in item and "body" in item and "permalink" in item:
            reqd_dict = {"author": item['author'], "body": ['body'], "permalink": ['permalink']}
            print("Found it!!")

【讨论】:

  • 答案通常不会以问题的形式写成。
  • 对不起,我第一次在 stackoverflow 上回答。你什么意思?我在其他地方回答吗?
  • 感谢@Ananya 的建议,但是 for item in hit 一次只检查一个字符。所以 if 语句中设置的条件总是假的。
  • @Zphere 是你使用的字符串吗?不要做 json.dumps。尝试命中变量 - json/dict 的变量,(不是 json.dump 字符串的 x)
  • 是的,你是对的,hit 是一个字符串。我让它工作了!感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2021-11-20
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多