json - 在powershell中重新排序对象成员
问题描述
我有以下用于特定测试的对象(它保存在 json 文件中并转换为对象):
TestID : 1510242
Path : example_designs/crr
2020/07/16 : _Failed_
2020/07/19 : _Failed_
2020/07/20 : _Failed_
07/22/2020 : _Failed_
07/23/2020 : _Passed_
07/26/2020 : _Passed_
07/27/2020 : _Pending_
07/28/2020 : _Passed_
07/29/2020 : _Passed_
07/30/2020 : _Running_
每天我添加一个成员
add-member -NotePropertyName $date -NotePropertyValue $testRsult
并将其转换回 Json 文件。
问题是新成员是在最后添加的,所以当我展示它时,最新的结果在最后。有没有为什么要重新排序成员,或在特定位置添加新成员?
解决方案
一个潜在的解决方案。而不是使用Add-Member
存储计算的属性。将其插入现有属性的数组中,然后在Select-Object
命令中引用该数组:
示例(在 PoSh 5.1 和 7.0 中测试):
$Date = (Get-Date).ToString()
$TestResult = "Some test result"
$NewPropertyExpression =
@{
Name = $Date.ToString()
Expression = { $TestResult }
}
$Object = [PSCustomObject]@{
Property1 = "1"
Property2 = "2"
Property3 = "3"
Property4 = "4"
Property5 = "5"
Property6 = "6"
Property7 = "7"
}
$Properties = [Collections.ArrayList]$Object.PSObject.Properties.Name
$Properties.insert( 2, $NewPropertyExpression )
$Object | Select-Object $Properties
所以上面我在数组的第二个索引处插入了一个属性。输出如下:
Property1 : 1
Property2 : 2
7/31/2020 12:25:41 PM : Some test result
Property3 : 3
Property4 : 4
Property5 : 5
Property6 : 6
Property7 : 7
注意:我在您编辑问题之前已经这样做了,但它应该仍然有效。如果我有时间,我会编辑以匹配。
您也可以在插入之前对数组进行排序。如果您想进一步讨论订购等...有很多选择。
推荐阅读
- regex - 必须释放创建的 System.RegularExpressions 对象的内存吗?
- reactjs - 如何在 React 的状态数组对象中显示嵌套值?
- ms-access - 查询中突然错误的运行总和值
- repast-simphony - 创建子类代理的问题
- highlight.js - 在 highlightjs 块中包含标记
- node.js - 由于 ng 问题,Angular 项目无法运行
- android - 如何在 Flutter 中为有条件渲染的小部件设置动画
- python - 在 Python 中对连续日期进行分组
- java - Complex.java 在 Intellij 中不被接受为 Java 类
- angular - Material - 将指令传递给子组件