powershell - 不能修改对象的属性
问题描述
我有以下文件,我将其读取为 Hashtable 并需要修改属性的值,但它未能这样做并出现错误InvalidOperation: The property 'apiVersion' cannot be found on this object. Verify that the property exists and can be set.
代码
$template = ConvertFrom-Json ([System.IO.File]::ReadAllText(".\template.json")) -AsHashtable;
$template.resources.apiVersion = "d"
JSON文件
{
"resources": [
{
"apiVersion": "2019-07-01"
}
]
}
解决方案
由于Resources
是一个数组,由[]
附件表示,因此您必须进入包含您的子属性的特定索引。
# if you know the index (0 in your example)
$template.resources[0].apiVersion = 'd'
# if you do not know the index
($template.resources | where {$_.ContainsKey('apiVersion')}).apiVersion = 'd'
推荐阅读
- python - 如何让 PyCharm 识别自定义属性装饰器?
- java - 在 Hibernate 的实体方法中使用命名查询
- xamarin - Xamarin 表单 TabbedPage
- javascript - 如何在jquery中添加和删除属性单击事件?
- ios - 允许用户使用来自另一个 viewController 的文本创建 tableViewCell?
- android - 如何更改我的应用程序中所有按钮的文本颜色?
- html - 如何根据用户输入从 XML 文件中检索数据并将其显示在 HTML 文件中?
- python-3.x - ImportError:没有名为“多处理”的模块
- c# - 在异步方法中等待事件
- flutter - 在颤动中格式化文本小部件的部分文本