【问题标题】:Python parse json keywordPython解析json关键字
【发布时间】:2017-05-08 23:38:01
【问题描述】:

我正在寻找解析网页上请求的一些数据。

到目前为止,我已经能够配置以下内容。

import urllib.request,json

with urllib.request.urlopen("JSONURL") as url:
   data = json.loads(url.read().decode())
   print(data["live-status-local"])

这会返回以下信息

{'status': 'error', 'status-description': '摄取选项:{srcname=a,ratelimit=3250000};后端:(空); src:文件IO错误; Push:由于最近的源信号问题,不尝试推送; ', 'status-description-debug': '', 'user-message': '源信号有问题', 'status-map': {'Ingest options': {'text': '{srcname=a, ratelimit=3250000}', 'level': 'unknown'}, 'Backend': {'level': 'ok'}, 'Src': {'text': 'File IO error', 'level': 'error '}, 'Push': {'text': '由于最近的源信号问题不尝试推送', 'level': 'error'}, 'BCC Setup': {'level': 'ok', ' debug': '会话 s3 已打开。 '}}, 'status-data': {'session-id': 's3', 'input.bitrate': '149'}}

如果 'status': 'error' 我想打印/读取

如果是这样,我希望系统返回说明存在错误。

有人可以帮我指出正确的方向吗?

【问题讨论】:

  • 您已经反序列化了您的 JSON,您正在使用 Python 对象。你在问如何使用dict
  • if data["live-status-local"]["status"] == "error":
  • 是的,我想专门检查那个字典项目。我是 Python 新手,所以这里的任何指针都会有所帮助。
  • 如果你刚开始使用 python,那么最好的办法就是查看tutorial。本网站并非用作教程。
  • @Barmar - 做到了。谢谢!

标签: python json python-3.x urllib


【解决方案1】:

我已经成功使用 .json()。导入 requests 后,尝试以下操作:

urlResults = requests.get("http://example.com/file.json")
data = urlResults.json()

if data["live-status-local"]["status"] == "error":
    #handle error

【讨论】:

    猜你喜欢
    • 2019-05-12
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2013-03-27
    • 2020-10-06
    • 1970-01-01
    • 2017-08-24
    • 2016-01-27
    相关资源
    最近更新 更多