gpt4 book ai didi

python - 类型错误 : Object of type WorkItem is not JSON serializable

转载 作者:行者123 更新时间:2023-12-03 04:59:47 34 4
gpt4 key购买 nike

运行下面的脚本后我收到此错误。我想通过此脚本中生成的结果创建一个 json 文件。我可以做什么来解决这个问题?

我尝试通过 API 执行此操作,但无法从此 DevOps 表中获取所需的字段。

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v5_1.work_item_tracking.models import Wiql
import json

token = 'xxx'
team_instance = 'https://dev.azure.com/xxx'

credentials = BasicAuthentication("", token)
connection = Connection(base_url=team_instance, creds=credentials)

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)

wit_client = connection.clients.get_work_item_tracking_client()

def get_TC_from_query(query):
query_wiql = Wiql(query=query)
results = wit_client.query_by_wiql(query_wiql).work_items
# WIQL query gives a WorkItemReference => we get the corresponding WorkItem from id
work_items = (wit_client.get_work_item(int(result.id)) for result in results)
print_work_items(work_items)

get_TC_from_query(
"""\
SELECT *
FROM workitems
WHERE
[System.TeamProject] = 'xxx'
and [System.WorkItemType] = 'Product Backlog Item'
and [System.State] = 'Done'
ORDER BY [System.ChangedDate] DESC
"""
)

TypeError: Object of type WorkItem is not JSON serializable

最佳答案

以下生成 generator ,其中 JSON 无法序列化:

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

您可以将 work_items 设为一个列表,JSON 可以对其进行序列化:

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

关于python - 类型错误 : Object of type WorkItem is not JSON serializable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60230820/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com