我得到了我的 xml 檔案,我想整理一些數字。
{
{
"Type": "bldr_chair",
"DisplayName": "bldr_chair",
"Position": [
4062.198974609375,
201.51414489746095,
3538.89501953125
],
"Orientation": [
0.0,
-0.0,
-0.0
],
"Scale": 1.0,
"Flags": 2147483647
},
{
"Type": "bldr_chair",
"DisplayName": "bldr_chair",
"Position": [
4100.68408203125,
200.10733032226563,
3418.51171875
],
"Orientation": [
0.0,
-0.0,
-0.0
],
"Scale": 1.0,
"Flags": 2147483647
},
{
"Type": "bldr_chair",
"DisplayName": "bldr_chair",
"Position": [
4448.24755859375,
201.4647979736328,
3362.69970703125
],
"Orientation": [
0.0,
-0.0,
-0.0
],
"Scale": 1.0,
"Flags": 2147483647
}
但我只需要
[4062.19897,201.51414,3538.89501],
這實際上就是我所擁有的。我嘗試對我的號碼進行排序或建立一個陣列,但在 powershell 中不容易實作所有需要達到的好處。
$input = Get-Content $path
$input | Select-String -pattern '\d \.\d{5}' -AllMatches | %{$_.matches} | %{$_.value
} | Add-Content $Ausgabepfad
我希望你能得到一些提示,我很感激你的幫助
uj5u.com熱心網友回復:
您的字串是 JSON 而不是 Mathias 指出的 XML,您不需要使用正則運算式來決議它。
$string = @'
"Position": [
4062.198974609375,
201.51414489746095,
3538.89501953125
],
'@
$json = "{ $string }" | ConvertFrom-Json
$json.Position | ConvertTo-Json -Compress
[4062.198974609375,201.51414489746094,3538.89501953125]
$pathpath 可能是一個有效的 Json 并且您可能不需要添加花括號{...},在這種情況下您只需執行以下操作:
(Get-Content $path | ConvertFrom-Json).Position | ConvertTo-Json -Compress
我相信你正在尋找這樣的東西:
$json.EditorObjects.ForEach({
$coords = foreach($coord in $_.Position) {
[math]::Round($coord, 3, 'AwayFromZero')
}
,$coords
}) | Select-Object -First 5 | ConvertTo-Json -Compress
結果是:
[[16112.0,578.507,4247.999],[5589.42,196.243,1670.624],[5448.696,193.072,2159.627],[5098.765,196.585,2797.101],[5418.692,197.421,2673.856]]
如果您不需要此 remove ,這將始終顯示為單行字串-Compress。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427380.html
