如何通过Python格式化JSON?

发布于 2025-02-09 09:30:19 字数 1376 浏览 1 评论 0原文

我有一个非常基本的问题,我无法弄清楚。试图将对象数据写入JSON文件时,我一直在获得“ Expect.json的终点”。我想知道如何解决这个问题?我通过写循环来做到这一点。不确定我如何格式化。

这是所讨论的代码

with open("data.json", "w") as outfile:
    for x,y in structures.infrastructures.items():
        outfile.write(Sector(x, y["Depended on by"],y["Depends on"], y["Sub sections"]).toJson())

输出

{
    "name": "Chemical",
    "depended_on": [
        "Critical Manufacturing",
        "Nuclear Reactors, Waste, and Material Management",
        "Commercial",
        "Healthcare and Public Health",
        "Food and Agriculture",
        "Energy"
    ],
    "depends_on": [
        "Emergency Services",
        "Energy",
        "Food and Agriculture",
        "Healthcare and Public Health",
        "Information Technology",
        "Nuclear Reactors, Waste, and Material Management",
        "Transportation Systems",
        "Water"
    ],
    "sub_sections": [
        "Chemical Plants",
        "Chemical Refineries",
        "Labs"
    ],
    "Status": 0,
    "Strain": 0
}{                                  -> this is where the error is
    "name": "Commercial",
    "depended_on": [
    ....
    ....
    etc

,这是我的tojson方法的

    def toJson(self):
        return json.dumps(self, default=lambda o: o.__dict__, indent=4)

:但是,是的,在以JSON格式编写对象数据的地方我该如何实现它?

I have a very basic problem that I can't figure out. I keep getting a "End of file expected.json" when trying to write object data to a json file. I was wondering how I can fix that? I do it by writing in a for loop. Not sure how I can format.

This is the code in question

with open("data.json", "w") as outfile:
    for x,y in structures.infrastructures.items():
        outfile.write(Sector(x, y["Depended on by"],y["Depends on"], y["Sub sections"]).toJson())

and this is the output

{
    "name": "Chemical",
    "depended_on": [
        "Critical Manufacturing",
        "Nuclear Reactors, Waste, and Material Management",
        "Commercial",
        "Healthcare and Public Health",
        "Food and Agriculture",
        "Energy"
    ],
    "depends_on": [
        "Emergency Services",
        "Energy",
        "Food and Agriculture",
        "Healthcare and Public Health",
        "Information Technology",
        "Nuclear Reactors, Waste, and Material Management",
        "Transportation Systems",
        "Water"
    ],
    "sub_sections": [
        "Chemical Plants",
        "Chemical Refineries",
        "Labs"
    ],
    "Status": 0,
    "Strain": 0
}{                                  -> this is where the error is
    "name": "Commercial",
    "depended_on": [
    ....
    ....
    etc

This is my toJson method:

    def toJson(self):
        return json.dumps(self, default=lambda o: o.__dict__, indent=4)

But yeah how can I implement it where my object data is written in JSON format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡 2025-02-16 09:30:19

有效的JSON文件只能包含一个对象。将您的数据收集到列表中,然后用一个调用编写,或用您的代码模拟格式。

A valid json file can only contain one object. Collect your data into a list and write it with a single call, or simulate the format with your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文