【发布时间】:2019-04-22 03:39:17
【问题描述】:
我已经看到很多关于此的问题,但是似乎没有一个答案有效(如果我错了,请纠正我)
我创建了一个查询 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
}
【问题讨论】:
-
顺便说一句:只使用
Format-*cmdlet 进行display 格式化;如果数据必须以编程方式处理,则永远不要使用它们。Format-*cmdlet 输出格式化指令,而不是数据 - 请参阅this answer.
标签: json powershell