为了账号安全,请及时绑定邮箱和手机立即绑定

在文本文件中写入一组 JSON 对象

在文本文件中写入一组 JSON 对象

白猪掌柜的 2021-10-19 16:12:52
我有一个空文件 data.json,我想用一个 json 数组填充它。我写了这个 Python 脚本:import jsonmyArray = []first = {}second = {}third = {}first["id"] = 1first["name"] = "Foo"second["id"] = 2second["name"] = "John" third["id"] = 2third["name"] = "Doe" myArray.append(first)myArray.append(second)myArray.append(third)with open('data.json', 'w') as my_file:    strinfgifyied_json_array = # A function that converts myArray to a stringifyed JSON array    my_file.write(strinfgifyied_json_array)my_file.close()我正在寻找一个将 JSON 对象数组转换为文本的函数,以便按如下所示编写它们:data.json 的内容:[{"id": 1, "name": "Foo"},{"id": 2, "name": "John"},{"id": 3, name: "Doe"}]
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 217 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号