简体   繁体   中英

TypeError: Object of type TextIOWrapper is not JSON serializable I don't know how to handle this

I want to parse this dictionary to a json file. I saw lots of solutions on google and on this forum but none of them worked.

import json


standardcategories = {
                "unnecessary": 0,
                "unimportant": 1,
                "important": 2,
                "crucial": 3,
                "fixed": 4
}
with open('categories.json','w') as f:
    json.dump(f,standardcategories,indent=2)

Use:

json.dump(standardcategories, f, indent=2)

Instead of:

json.dump(f, standardcategories, indent=2)

Notice that I've interchanged the order of f and standardcategories

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM