json - 如何在powershell中选择具有特殊字符的键?
问题描述
我有一组来自 JSON 文件的键/值对。
$ $p.dependencies
@architect/architect : ^5.7.0
@architect/functions : ^3.0.4
assert : ^1.4.1
bcrypt : ^3.0.6
find-parent-dir : ^0.3.0
hashids : ^1.2.2
http-status-codes : ^1.3.2
lodash.get : ^4.4.2
mini-web-server : ^1.0.2
mini-webhook-server : ^1.0.4
mocha : ^5.2.0
opencorporates : ^3.0.0
stripe : ^6.23.1
uuid : ^3.3.2
whois-json : ^2.0.4
看起来在 Powershell 中它们是noteProperty
:
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
@architect/architect NoteProperty string @architect/architect=^5.7.0
@architect/functions NoteProperty string @architect/functions=^3.0.4
assert NoteProperty string assert=^1.4.1
bcrypt NoteProperty string bcrypt=^3.0.6
find-parent-dir NoteProperty string find-parent-dir=^0.3.0
hashids NoteProperty string hashids=^1.2.2
http-status-codes NoteProperty string http-status-codes=^1.3.2
lodash.get NoteProperty string lodash.get=^4.4.2
mini-web-server NoteProperty string mini-web-server=^1.0.2
mini-webhook-server NoteProperty string mini-webhook-server=^1.0.4
mocha NoteProperty string mocha=^5.2.0
opencorporates NoteProperty string opencorporates=^3.0.0
stripe NoteProperty string stripe=^6.23.1
uuid NoteProperty string uuid=^3.3.2
whois-json NoteProperty string whois-json=^2.0.4
我想将带有键的项目更改为@architect/functions
不同于的值^3.0.4
(然后将数据保存回来)
下一部分似乎是从哈希表中选择正确的项目。我在用:
$p.dependencies | where $_.key -eq "@architect/functions"
但是,这不会返回任何结果。如何使用键选择项目@architect/functions
?对于奖励积分,我该如何更改价值!
编辑:
使用答案,这是我的最终脚本,如果有人觉得它有用的话。
ls 'src/http' | foreach {
$packageJSONFile = "${PSItem}\package.json"
$packageJSON = cat $packageJSONFile | convertfrom-json
if ( $packageJSON.dependencies.'@architect/functions' ) {
$packageJSON.dependencies.'@architect/functions' = '^3.0.4'
}
$packageJSON | ConvertTo-Json -depth 100| set-content $packageJSONFile
}
解决方案
要获取名称中包含特殊字符的属性,请引用它:
$p.dependencies.'@architect/functions'
推荐阅读
- css - ngfor 循环中的按钮禁用/启用
- mirth - 是什么导致 Mirth 向出站 IP 和端口发送“RST”重置命令?
- git - Visual Studio Enterprise Git 合并完成但有冲突
- node.js - 繁琐的请求只能在 LoggedIn 状态下进行
- unreal-engine4 - 如何将 UE4 项目附加到 CV?
- node.js - 我在 Node + expressjs 应用程序中集成了 tinymce
- kdb - 如何使用 wj 在表中存储时间戳列表
- r - 如何在 R 版本的 tensorflow 中实现积分梯度?
- python - Matplotlib 分组图例项
- html - 是否在主标题之前使用副标题来介绍对 SEO 不利的部分?