【问题标题】:Powershell: How to Update/Replace data and values in Json and XML ObjectPowershell:如何更新/替换 Json 和 XML 对象中的数据和值
【发布时间】:2017-12-17 10:48:45
【问题描述】:

所以我在这里遇到了一点问题,我似乎无法弄清楚如何更新对象中的数据值

比如说下面的 json

{
    "People": 263,
    "Hungry": true,
    "Fruits": {
        "Apples": 1 "Oranges": 2
    },
    "Places": {
        "Places": [{
                "Baskets": "true",
                "name": "Room 1",
                "candycount": 1500,
                "candytypespresent": {
                    "candies": [
                        "caramel"
                    ]
                }

            },
            {

                "Baskets": "false",
                "name": "Room 2",
                "candycount": 2000,
                "candytypespresent": {
                    "candies": [
                        "caramel",
                        "jawbreaker",
                        "butterscotch"
                    ]
                }
            }
        ]
    }
}

我让 Powershell 用convertfrom-json顺利阅读

我将如何执行以下操作:

A) 将“橙子”从“2”改为“100”

B) 房间 2 中的“篮子”从“假”变为“真”

C) 将“泡泡糖”添加到 Room1 中的“糖果”

如何在不重写整个 json 或对象的情况下更新它

【问题讨论】:

    标签: json powershell object updates


    【解决方案1】:

    JSON 变成了一个带有嵌套对象的自定义对象,所以真的很简单。首先,让我们通过在 Apples 值后添加逗号来修复 JSON,并将其转换为对象...

    $JSON = @'
    {
    "People":  263,
    "Hungry":  true,
    "Fruits":  {
                    "Apples":  1,
                    "Oranges":  2
                },
    "Places":  {
                  "Places":  [
                                {
                                    "Baskets":  "true",
                                    "name":  "Room 1",
                                    "candycount":  1500,
                                    "candytypespresent":  {
                                                         "candies":  [
                                                                         "caramel"
                                                                     ]
                                                     }
    
                                },
                                {
    
                                    "Baskets":  "false",
                                    "name":  "Room 2",
                                    "candycount":  2000,
                                    "candytypespresent":  {
                                                         "candies":  [
                                                                        "caramel",
                                                                        "jawbreaker",
                                                                        "butterscotch"                                                                    
                                                                    ]
                                                    }
                                }
                            ]
              }
    }
    '@ | ConvertFrom-JSON
    

    如果我们想将 Oranges 从 2 更新到 100,我们只需更改值:

    $JSON.Fruits.Oranges = 100
    

    同样,我们可以通过简单地列出 Places 来修改 Room 2,将其传递给 Where 语句以获取正确的房间,并在 ForEach 循环中修改值。

    $JSON.Places.Places | Where{$_.name -eq 'Room 2'} | ForEach{$_.Baskets = 'true'}
    

    最后,由于candies 被定义为 JSON 中的数组,我们可以简单地将所需的糖果添加到数组中。

    $JSON.Places.Places | Where{$_.name -eq 'Room 1'} | ForEach{$_.CandyTypesPresent.candies += 'bubblegum'}
    

    【讨论】:

    • AAAAAAH 就是这么简单...只是一个管道foreach !!!我非常狭隘地寻找并试图过度设计这个......谢谢
    • 但是当我设置内容时,它破坏了我的 json 结构。
    • @themadtechnician 你如何将新对象添加到地点:$JSON.Places.Places[0].Color
    • 如果您有要先添加到其中的对象,则最简单,例如$Color = 'Red'$Color = 'Red','Blue','Yellow'。然后你使用Add-Member 就像:Add-Member -InputObject $JSON.Places.Places[0] -Notepropertyname 'Color' -Notepropertyvalue $Color
    猜你喜欢
    • 2021-06-14
    • 1970-01-01
    • 2018-01-02
    • 2022-01-11
    • 2022-11-17
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多