简体   繁体   English

将字典列表写入 Json 文件时出错

[英]Getting error while writing list of dictionaries to Json File

Trying to write list of dictionaries to Json File using below code but getting error.尝试使用以下代码将字典列表写入 Json 文件但出现错误。

Input输入

lst :  [{ '_id': ObjectId('620b7bb7d174648bd95cb105'), 'Name': 'Naveen',  'Id': '19', 'Session_Id': '123456'}]

Code Tried代码试过

with open('output.json', 'w') as fout:
    json.dump(lst, fout)

Error Getting获取错误

TypeError: Object of type ObjectId is not JSON serializable

Your input does not seem like valid JSON to me, since you are missing the { } brackets to state that it is JSON. Also, after the name "Naveen" , you are missing another ' .您输入的 JSON 对我来说似乎无效,因为您缺少 state 的{ }括号,它是 JSON。此外,在名称"Naveen"之后,您还缺少另一个'

{
   "list":[
      {
         "Name":"Naveen",
         "Id":"19",
         "Session_Id":"123456"
      }
   ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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