繁体   English   中英

修改Powershell 4中的现有JSON文件

[英]Amending Existing JSON Files in Powershell 4

我已经看到许多与此相关的问题,但是似乎没有答案(如果我错了,请纠正我)

我创建了一个查询API的函数,并将结果另存为json文件。
但是我想修改保存的文件

powershell代码:

$search = ""


function searchMtgApi($searchName){
    $uriSafeName = [uri]::EscapeDataString($searchName)
    $res = Invoke-WebRequest "https://api.magicthegathering.io/v1/cards?name=$uriSafeName" -UseBasicParsing
    $resJson = $res.content | ConvertFrom-Json
    $resJson.cards | Format-Table
    $resJson.cards | Select name, setName, "quantity" | ConvertTo-Json | Out-File "D:\Magic the Gathering\$searchName.json"
}

while ($search -ne "exit"){
    Write-Host @'

To exit, type "Exit".

'@
    $search = Read-Host "Search Magic the Gathering.IO by card name"
    if($search -ne "exit"){
          searchMtgApi $search
    }
}

生成Json(搜索卡“ Ponder”)

[
    {
        "name":  "Ponder",
        "setName":  "Commander 2018",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Lorwyn",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic 2010",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic 2012",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic Player Rewards 2008",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic Online Promos",
        "quantity":  null
    }
]

我想做的是加载文件并修改特定集合的“数量”。
谁能指出我正确的方向?

  • 更新-

我将从文件中获取json内容:

function updateJsonFile($jsonfile){
    $resJson = Get-Content "$jsonfile.json" | ConvertFrom-Json 
    #Edit Quantity here
    $resJson | ConvertFrom-Json | Format-Table
}

仅关注核心问题,并提供简短的样本输入:

以下内容有选择地更新具有给定setName属性值的对象的quantity属性,并从JSON转换为JSON:

$setName = 'Magic 2010' # the set name of interest
$newQuantity = 42       # new quantity

(@'
[
    {
        "name":  "Ponder",
        "setName":  "Commander 2018",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Lorwyn",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic 2010",
        "quantity":  null
    }
]
'@  | ConvertFrom-Json) | 
  ForEach-Object {
    if ($_.setName -eq $setName) {
        $_.quantity = $newQuantity
    }
    $_ # pass the (possibly updated) object through.
  } | ConvertTo-Json

请注意,需要将ConvertFrom-Json调用括在(...) ,这将强制枚举由JSON输入构造的数组中的各个对象(有关背景信息,请参见此答案 )。

上面的产量(注意更新的最后quantity值):

[
  {
    "name": "Ponder",
    "setName": "Commander 2018",
    "quantity": null
  },
  {
    "name": "Ponder",
    "setName": "Lorwyn",
    "quantity": null
  },
  {
    "name": "Ponder",
    "setName": "Magic 2010",
    "quantity": 42
  }
]

应用于您的updateJsonFile函数:

function updateJsonFile($jsonfile, $setName, $newQuantity){
    $resJson = Get-Content "$jsonfile.json"
    # Note the (...) around the ConvertFrom-Json command.
    ($resJson | ConvertFrom-Json) | 
      ForEach-Object {
        if ($_.setName -eq $setName) {
          $_.quantity = $newQuantity
        }
        $_ # pass the (possibly updated) object through.
      } | ConvertTo-Json | Set-Content -Encoding Utf8 $jsonfile
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM