gpt4 book ai didi

json - 使用Powershell读取Json字符串

转载 作者:行者123 更新时间:2023-12-03 00:55:58 26 4
gpt4 key购买 nike

我有一个JSON字符串如下。

$json='        {
"code": 0,
"ms": "success",
"dt": {
"st": 1,
"mns": [
"@{name=product 1 - w; att=response_time; st=1; time=2020-08-21T14:18:03-0400; locations=System.Object[]; attN=RESPONSETIME; mt=PORT; attL=Response Time; attribute_value=67; unit=ms; monitor_id=12}",
"@{name=product 2 - w; att=response_time; st=1; time=2020-08-21T14:18:03-0400; locations=System.Object[]; attN=RESPONSETIME; mt=PORT; attL=Response Time; attribute_value=70; unit=ms; monitor_id=23}",
"@{name=product 3 - w; att=response_time; st=1; time=2020-08-21T14:18:03-0400; locations=System.Object[]; attN=RESPONSETIME; mt=PORT; attL=Response Time; attribute_value=66; unit=ms; monitor_id=24}",
"@{st=5; name=product 4 - w; mt=PORT; monitor_id=35}"
],
"gd": "12345",
"gn": "group_name 5 - w"
}
}'
我尝试使用以下代码读取“mns”下给出的状态值
    $jsonobject= ConvertFrom-Json -InputObject $json
foreach ($mn in $jsonobject.dt.mns)
{
$mndata=ConvertFrom-StringData -StringData $mn
Write-Host $mndata["st"]
}
但是我无法检索$ mndata [“st”]的值。

最佳答案

ConvertFrom-StringData期望键值对在单独的行上,因此您必须先重新格式化输入字符串:

foreach ($mn in $jsonobject.dt.mns)
{
$multiLine = $mn.Trim('@{};') -replace ';\s*',"`r`n"
$mndata = ConvertFrom-StringData -StringData $multiLine
Write-Host $mndata["st"]
}

给定字符串(和 locations=System.Object[]对)的格式,看来原始JSON可能是由 ConvertTo-Json生成的,但 -Depth参数不足,您可能会更容易地解决此问题,只需返回并修复即可来自的数据

关于json - 使用Powershell读取Json字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63556911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com