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.Oragnes = 100
类似地,我们可以通过简单地列出场所,将其传递给
Where语句以获取正确的房间并修改其值来修改Room 2
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'}


