json - 无法使用 python 读取 json 文件。获取类型错误 : json object is 'TextIOWrapper'

标签 json python-3.x

我正在尝试从 json 文件中读取。

这是我创建文件的方式:

import requests
import json
import time
from pprint import pprint

BASE_URL = "https://www.wikiart.org/en/api/2/UpdatedArtists"
artist_json_data = requests.get(BASE_URL).json()

with open('artistdata.json', 'w') as outfile:
    while artist_json_data['hasMore']:
        print(artist_json_data['paginationToken'])
        url = BASE_URL + "?paginationToken=" +artist_json_data['paginationToken']
        artist_json_data = requests.get(url).json()
        json.dump(artist_json_data, outfile, indent=4)
        time.sleep(1)

这是我输出的开始:
{
    "data": [
        {
            "id": "57726da5edc2cb3880b4ca54",
            "artistName": "Paul Feeley",
            "url": "paul-feeley",
            "lastNameFirst": "Feeley Paul",
            "birthDay": "/Date(-1893456000000)/",
            "deathDay": "/Date(-126230400000)/",

当我尝试使用以下代码读取同一个文件时:
from pprint import pprint

with open('artistdata.json', 'r', encoding='utf-8') as data_file:    
    data = json.loads(data_file)
    pprint(data)

我收到错误
TypeError: the JSON object must be str, bytes or bytearray, not 'TextIOWrapper'

我不明白,因为我可以像往常一样在 sublime 中打开文件。我该如何处理?

用以下代码解决了这个问题:

问题是我混合了转储和负载。现在我正在使用转储和加载
class Wikiart:
    '''Class to access wikiart.org Data'''
    def __init__(self):
        self.BASE_URL = "https://www.wikiart.org/en/"
        self.BASE_URL_API = self.BASE_URL + "api/2/"
        self.BASE_URL_MOVEMENT = self.BASE_URL + 'artists-by-art-movement/'
        self.ARTIST_DATA_URL = self.BASE_URL_API + "UpdatedArtists"

    def write_artist_data_into_json_file(self):
            artists = requests.get(ARTIST_DATA_URL).json()
            all_artists = artists['data']

            with open('artistdata.json', 'w') as outfile:
                while artists['hasMore']:
                    print('fetching next: pagination token',artists['paginationToken'])
                    url = BASE_URL + "?paginationToken=" + artists['paginationToken']
                    artists_next_page = requests.get(url).json()
                    next_artists = artists_next_page['data']
                    time.sleep(0.25)
                    all_artists = all_artists + next_artists
                    artists = artists_next_page
                json.dump(all_artists, outfile, indent=4)

from pprint import pprint

with open('artistdata.json', 'r', encoding='utf-8') as data_file:    
    data = json.load(data_file)
    pprint(data)

最佳答案

json.load()用于加载文件。 json.loads()与字符串一起使用。

关于json - 无法使用 python 读取 json 文件。获取类型错误 : json object is 'TextIOWrapper' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44035799/

相关文章:

javascript - 从 AngularJS 中的 JQuery 读取文件

python-3.x - 如何使用 asyncio 优雅地超时

python - Selenium Chromedriver - 单击取消 Chrome 身份验证弹出窗口

javascript - Three.js - 更新纹理

php - 基于主键从 mysqli 查询创建 JSON

python - 用于检查元素是否在排序列表中的递归函数

regex - 如果未找到先前的模式,则忽略 python 正则表达式中的后续模式

shell - 如何读取tomcat SSL证书过期日期

jquery - 语言切换 - 使用 HTML 而不是 Key

javascript - AngularJS(前端)和 Laravel(后端)中 JSON/Arrays 中的 NULL