P
P
Python Newbie2021-08-29 19:09:09
Python
Python Newbie, 2021-08-29 19:09:09

Error with JSON? How to decide?

There is a code:

import requests
import json




#1 Приём сообщений
#2 Отсылка сообщений



URL = 'https://api.telegram.org/botтокен скрыт/'





def write_json(data, filename='answer.json'):
    with open(filename, 'w') as f:
        json.dump(data, f, indent=2, ensure_ascii=False)
        
def get_updates():
    url = URL + 'getUpdates'
    r = requests.get(url)
    write_json(r.json)

def main():
    get_updates()





if __name__ == '__main__':
    main()


When I run it on windows I get the following error:
Traceback (most recent call last):
  File "C:\Users\Дом\Desktop\Py\PY\flaskapp\flaskbot.py", line 41, in <module>
    main()
  File "C:\Users\Дом\Desktop\Py\PY\flaskapp\flaskbot.py", line 34, in main
    get_updates()
  File "C:\Users\Дом\Desktop\Py\PY\flaskapp\flaskbot.py", line 31, in get_updates
    write_json(r.json)
  File "C:\Users\Дом\Desktop\Py\PY\flaskapp\flaskbot.py", line 26, in write_json
    json.dump(data, f, indent=2, ensure_ascii=False)
  File "C:\Users\Дом\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 179, in dump
    for chunk in iterable:
  File "C:\Users\Дом\AppData\Local\Programs\Python\Python38\lib\json\encoder.py", line 438, in _iterencode
    o = _default(o)
  File "C:\Users\Дом\AppData\Local\Programs\Python\Python38\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type method is not JSON serializable


What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-08-29
@Levman5

json - method
r.json()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question