首页 > 解决方案 > 将列表存储在 json 数据中

问题描述

我想要一个 json 文件,该文件具有tested_versions存储工具列表及其版本的主键

{
    "tested_versions": [{
        'version': 1.2,
        'tool': 'superb'
    }]
}

但是当我尝试使用json.dump(file_obj, json_data)

我收到错误消息

TypeError: <open file '/home/zchang/log/tested_versions.json', mode 'w' at 0x36a09c0> is not JSON serializable

标签: pythonjson

解决方案


也许你必须在 json.dump() 中切换参数的顺序

with open('file.json', 'w') as file_obj:
    json.dump(json_data, file_obj)

推荐阅读