TypeError: Object of type WorkItem is not JSON serializable

前端 未结 2 1931
你的背包
你的背包 2021-01-25 22:23

I received this error after running the script below. I would like to create a json file through the result that is generated in this script. What can I do to correct this probl

2条回答
  •  余生分开走
    2021-01-25 23:08

    Inspired by the thread provided by @jordanm. You can override the default() method to serialize additional types.

    I made below changes to your code, and it worked when i tested.

    First make work_items a list:

    work_items = [wit_client.get_work_item(int(result.id)) for result in results]

    Then in print_work_items method, i override the default method in json.dump() with default = lambda o: o.__dict__. Please check below:

    def print_work_items(work_items):
        for work_item in work_items:
            with open('teste_project.json', 'w') as json_file:
                json.dump(work_items, json_file, default = lambda o: o.__dict__, sort_keys=True, indent=4)
    

提交回复
热议问题