簡體   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