移除/删除 json 值(多个数组)

发布于 2025-01-19 20:43:30 字数 597 浏览 0 评论 0原文

我想在数组中删除“软件3”。我对JSON数据很新,我不知道正确的方法是什么。我尝试了未设置的方法,但它不起作用。谁能告诉我这个例子。我已经搜索了该解决方案,但其中大多数没有使用多个数组。

JSON数据:

"system_info": {
"hardware": {
  "model": "PowerEdge R710",
  "serialno": "FG6MC2S",
  "warranty": {
    "warranty_start_date": "2022-03-14",
    "warranty_end_date": "2025-03-14"
  }
},
"operating_env": {
  "os": "PowerEdge R710",
  "os_version": "FG6MC2S"
},
"software_installed": [
  {
    "name": "Software 1",
    "version": "4.5"
  },
  {
    "name": "Software 2",
    "version": "5.5"
  },
  {
    "name": "Software 3",
    "version": "5.5"
  }
]},

I want to remove 'Software 3' in the array. I'm quite new with json data and I do not know what is the correct way to do it. I have tried the unset method but its not working. Can anyone show me the example. I already search the solution but most of it not using multiple array.

json data:

"system_info": {
"hardware": {
  "model": "PowerEdge R710",
  "serialno": "FG6MC2S",
  "warranty": {
    "warranty_start_date": "2022-03-14",
    "warranty_end_date": "2025-03-14"
  }
},
"operating_env": {
  "os": "PowerEdge R710",
  "os_version": "FG6MC2S"
},
"software_installed": [
  {
    "name": "Software 1",
    "version": "4.5"
  },
  {
    "name": "Software 2",
    "version": "5.5"
  },
  {
    "name": "Software 3",
    "version": "5.5"
  }
]},

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

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

发布评论

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

评论(2

鱼忆七猫命九 2025-01-26 20:43:30

使用JavaScript

const data = {
  system_info: {
    hardware: {
      model: 'PowerEdge R710',
      serialno: 'FG6MC2S',
      warranty: {
        warranty_start_date: '2022-03-14',
        warranty_end_date: '2025-03-14',
      },
    },
    operating_env: {
      os: 'PowerEdge R710',
      os_version: 'FG6MC2S',
    },
    software_installed: [
      {
        name: 'Software 1',
        version: '4.5',
      },
      {
        name: 'Software 2',
        version: '5.5',
      },
      {
        name: 'Software 3',
        version: '5.5',
      },
    ],
  },
};
const { system_info: { hardware, operating_env, software_installed } } = data;
const newData = {
  system_info: {
    hardware,
    operating_env,
    software_installed: software_installed.filter(s => s.name !== 'Software 3')
  }
}
console.log(newData);

Using javascript

const data = {
  system_info: {
    hardware: {
      model: 'PowerEdge R710',
      serialno: 'FG6MC2S',
      warranty: {
        warranty_start_date: '2022-03-14',
        warranty_end_date: '2025-03-14',
      },
    },
    operating_env: {
      os: 'PowerEdge R710',
      os_version: 'FG6MC2S',
    },
    software_installed: [
      {
        name: 'Software 1',
        version: '4.5',
      },
      {
        name: 'Software 2',
        version: '5.5',
      },
      {
        name: 'Software 3',
        version: '5.5',
      },
    ],
  },
};
const { system_info: { hardware, operating_env, software_installed } } = data;
const newData = {
  system_info: {
    hardware,
    operating_env,
    software_installed: software_installed.filter(s => s.name !== 'Software 3')
  }
}
console.log(newData);
难如初 2025-01-26 20:43:30

使用Python。我假设该数据位于 data.json

remove_script.py

import json

# Opening JSON file
with open('data.json') as json_file:
    data = json.load(json_file)

    # Remove software 3
    data['system_info'].update(
        {"software_installed": data['system_info']['software_installed'][:2]})

    print(data)

    # write to another file
    with open("output.json", "w") as write_file:
        json.dump(data, write_file, indent=4)

打印输出

>>> {'system_info': {'hardware': {'model': 'PowerEdge R710', 'serialno': 'FG6MC2S', 'warranty': {'warranty_start_date': '2022-03-14', 'warranty_end_date': '2025-03-14'}}, 'operating_env': {'os': 'PowerEdge R710', 'os_version': 'FG6MC2S'}, 'software_installed': [{'name': 'Software 1', 'version': '4.5'}, {'name': 'Software 2', 'version': '5.5'}]}}

output.json中

{
    "system_info": {
        "hardware": {
            "model": "PowerEdge R710",
            "serialno": "FG6MC2S",
            "warranty": {
                "warranty_start_date": "2022-03-14",
                "warranty_end_date": "2025-03-14"
            }
        },
        "operating_env": {
            "os": "PowerEdge R710",
            "os_version": "FG6MC2S"
        },
        "software_installed": [
            {
                "name": "Software 1",
                "version": "4.5"
            },
            {
                "name": "Software 2",
                "version": "5.5"
            }
        ]
    }
}

Using python. I will assume that this data lives in data.json

remove_script.py

import json

# Opening JSON file
with open('data.json') as json_file:
    data = json.load(json_file)

    # Remove software 3
    data['system_info'].update(
        {"software_installed": data['system_info']['software_installed'][:2]})

    print(data)

    # write to another file
    with open("output.json", "w") as write_file:
        json.dump(data, write_file, indent=4)

printed output

>>> {'system_info': {'hardware': {'model': 'PowerEdge R710', 'serialno': 'FG6MC2S', 'warranty': {'warranty_start_date': '2022-03-14', 'warranty_end_date': '2025-03-14'}}, 'operating_env': {'os': 'PowerEdge R710', 'os_version': 'FG6MC2S'}, 'software_installed': [{'name': 'Software 1', 'version': '4.5'}, {'name': 'Software 2', 'version': '5.5'}]}}

output.json

{
    "system_info": {
        "hardware": {
            "model": "PowerEdge R710",
            "serialno": "FG6MC2S",
            "warranty": {
                "warranty_start_date": "2022-03-14",
                "warranty_end_date": "2025-03-14"
            }
        },
        "operating_env": {
            "os": "PowerEdge R710",
            "os_version": "FG6MC2S"
        },
        "software_installed": [
            {
                "name": "Software 1",
                "version": "4.5"
            },
            {
                "name": "Software 2",
                "version": "5.5"
            }
        ]
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文