【发布时间】: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})
我尝试使用
转换为 JSONx = 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