python - 从 json 文件中读取 int

标签 python json

我是 Python 新手,我正在尝试从我的网站获取大量用户信息并保存每个人拥有的用户组

我已经能够将响应保存为 json,但我不知道如何读取返回的内容

我的 json 文件

{
    "pagination": {
        "current_page": 1,
        "last_page": 9,
        "per_page": 20,
        "shown": 20,
        "total": 173
    },

我不想显示太多,因为我的文件中保存了电子邮件等。

具体来说,我希望保存“total”的值

尝试

with open(pathFile) as json_file:

    # read json file line by line
    for line in json_file.readlines():

        # create python dict from json object
        json_dict = json.loads(line)

        # check if "body" (lowercased) contains any of the keywords
        if any(keyword in json_dict["total"].lower() for keyword in keywords):
            print(json_dict["pagination"])

错误消息

Exception has occurred: JSONDecodeError
Expecting property name enclosed in double quotes: line 2 column 1 (char 2)
  File "C:\Users\New User\Desktop\path thing", line 43, in <module>
    json_dict = json.loads(line)

最佳答案

不要尝试读取每一行,只需在整个文件上使用 json.load 即可。

import json

with open(filePath, "r") as json_file:
    data = json.load(json_file)

total = data["pagination"]["total"]
print(total)

关于python - 从 json 文件中读取 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66373503/

相关文章:

python - 如何监控我的 gtalk 状态消息的变化?

python - 取代按钮的 Kivy 可点击图像

python - 读取 JSON 并将键转换为 int

javascript - 正则表达式匹配文本中的简单 json

android - 创建一个 JSON 对象数组

python - GAE 中的 worker 角色和 Web 角色对应

python - 通过django url传递2个变量

Python 在分隔符上分割字符串,但不在此类分隔符的某些变体上分割字符串

python - 返回非 json 对象作为 lambda 响应

python - 为什么python json模块在同一个文件上产生不同的编码