json - 在 Powershell 4 中修改现有 JSON 文件
问题描述
我已经看到了很多关于此的问题,但是似乎没有一个答案有效(如果我错了,请纠正我)
我创建了一个查询 API 并将结果保存为 json 文件的函数。
但是我想修改保存的文件
电源外壳代码:
$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 生成(搜索卡片“思考”)
[
{
"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
}
解决方案
仅关注核心问题,并提供删节的示例输入:
下面选择性地更新quantity
具有给定属性值的对象的setName
属性,从 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
}
推荐阅读
- reactjs - 在反应中为数字类型初始化一个空值
- python - JSONDecodeError:期望用双引号括起来的属性名称:第 1 行第 3 列(字符 2)
- json - 使用 PowerShell 从单元素 JSON 数组对象创建 CSV 输出
- websocket - socket io 客户端返回断开连接
- c++ - 使用win32 C++显示文件夹中的所有文件
- python - 在初学者函数递归练习中的 Python 输出中得到一些意想不到的“无”
- javascript - 如何使用 puppeteer 从节点 js 中接收的数据中删除换行符
- c - 在 C 中,如何将字符串存储在多维表中?
- python - Pycharm 中的 OPENCV 向我展示了一个数组值错误
- javascript - 为什么我的 script.js 文件不起作用,但 index.html 文件中的 JavaScript 代码起作用?