【问题标题】:How to read a particular field from json data? [closed]如何从 json 数据中读取特定字段? [关闭]
【发布时间】:2020-02-02 00:56:07
【问题描述】:

我想从 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,
}

【问题讨论】:

  • 请贴出您目前尝试过的代码和遇到的错误(Python 中有一个 json 模块可以做到这一点)。

标签: python json


【解决方案1】:

Python 包含 json 模块,它允许您将 JSON 字符串直接解析为 Python 字典。

import json
dic = json.loads(json_str)

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

请记住,必须使用有效的JSON 格式。
您必须删除"favorite_count":0, 末尾的','

【讨论】:

  • 嘿,我收到了这个错误:“ValueError: Trailing data”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2020-04-27
  • 2014-10-25
  • 2019-06-05
  • 1970-01-01
  • 2022-01-20
  • 2021-12-13
相关资源
最近更新 更多