关于write-host的小问题, write-host为什么得不到正确的结果


PowerShell交流中心分类: Questions关于write-host的小问题, write-host为什么得不到正确的结果
0
周蒙牛 asked 7 年 ago

$path= Resolve-Path “./test.json”
$json=ConvertFrom-Json(Get-Content($path) | Out-String)

$folder=”**”+$json.name+”**”
write-host $folder #Desired result 这个才能得到我要的结果, 下面三个都不行

write-host “**”+$json.name+”**”

write-host “**”+($json.name)+”**”

write-host “**”($json.name)”**”

 
#output:
**reddit**
** +@{date_added=13145624060145036; id=568; name=reddit; sync_transaction_version=3272; type=url; url=https://www.reddit.com/}.name+**
** + reddit +**
** reddit **
 
#test.json

{
“date_added”: “13145624060145036”,
“id”: “568”,
“name”: “reddit”,
“sync_transaction_version”: “3272”,
“type”: “url”,
“url”: “https://www.reddit.com/”
}

 

1 Answers
1
Best Answer
Mooser Lee 管理员 answered 7 年 ago

我给你演示两种正确的方式:
PS> $obj = ConvertFrom-Json (Get-Content .\one.json -Raw)
PS> $Obj
Name Id
—- —
Mosser 1
PS> Write-Host “**$($obj.Name)**”
**Mosser**
PS> Write-Host (“**”+$Obj.Name+”**”)
**Mosser**

jin7 replied 7 年 ago

多谢哈 ! 棒