如何从json数据中读取特定字段?

2024-06-01 01:37:20 发布

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

我想从JSON数据中检索"text:"字段。在python中如何做到这一点?你知道吗

{ 
   "contributors":null,
   "truncated":false,
   "text":"A perfect gift to yourself this new year! #celiac #glutenfree ",
   "in_reply_to_status_id":null,
   "id":418468308673773568,
   "favorite_count":0,
}

Tags: to数据textidjsonfalsenewthis
1条回答
网友
1楼 · 发布于 2024-06-01 01:37:20

Python包含^{}模块,它允许您将JSON字符串直接解析到Python字典中。你知道吗

import json
dic = json.loads(json_str)

现在可以使用dic['key']访问该值。在你的情况下dic['text']。你知道吗

请记住,有效的JSON格式是必要的。
必须删除','末尾的"favorite_count":0,。你知道吗

相关问题 更多 >