如何将invoke-request的结果转换为多维数组?

时间:2019-05-29 20:35:28

标签: json powershell invoke-webrequest

我使用了“ Invoke-request”来获取JSON消息。我想将其转换为多维数组。我在网上看了一下,但找不到合适的解决方案。答案是肯定的,如果不是,那么给我指出适当的资源也会有所帮助。

谢谢

{"ResultForName":
    [{"Name":"work","Values":["no"]},
    {"Name":"location","Values":["\\pc\\documents"]},
    {"Name":"Authority","Values":["10"]},
    {"Name":"City","Values":["Toronto"]},
    {"Name":"Country","Values":["CA"]},
    {"Name":"Created By","Values":["George"]},
    {"Name":"Money Vault Id","Values":["100000"]},
    {"Name":"Opened  On","Values":["Mark, John, L=Toronto, S=Ontario, C=CA","www.doman.com", O=Office, L=Toronto, S=Ontario, C="CA"]},
                    {"Name":"Driver Name","Values":["Honda"]},
                    {"Name":"Can drive","Values":["0"]},
                    {"Name":"Given Name","Values":["Steven"]},
                    {"Name":"Group Id","Values":["34324"]},
                    {"Name":"Internet System Address","Values"["www.doman.com/Locations"]},
                    {"Name":"Key value","Values":["AXU"]},
                    {"Name":"Key Encryption","Values":["128"]},
                    {"Name":"Last Check up","Values":["12\/20\/2018 04:00:08"]},
                    {"Name":"Last Notification","Values":["11\/22\/2018 05:00:59"]},
                    {"Name":"Last used by","Values":["David"]},
                    {"Name":"Last Used On","Values":["09\/11\/2018 14:57:50"]},
                    {"Name":"Validation","Values":["02\/22\/2019 04:06:18"]},
                    {"Name":"Last Validation query","Values":["Success"]},
                    {"Name":"Last Acceptence","Values":["04\/12\/2019 04:10:11"]},
                    {"Name":"Managed By","Values":["Manager"]},
                    {"Name":"Automatic","Values":["True"]},
                    {"Name":"Options","Values":["More options Needed"]},
                    {"Name":"Organization","Values":["Organization"]},
                    {"Name":"Organizational Unit","Values":["CSV"]},
                    {"Name":"Work Location Date","Values":["02\/29\/2019 02:00:40"]},
                    {"Name":"URL Details","Values":["CN=web.website.com,O=\"Organization\",OU=CSV,L=Toronto,ST=Ontario,C=CA"]},
                    {"Name":"Provence","Values":["Ontario"]},
                    {"Name":"Surname","Values":["Johnson"]},
                    {"Name":"Transaction value","Values":["23452523"]},
                    {"Name":"Validation Acceptance Values",
                                    "Values":["111.11.11.11|443|0|11|0|organization.com\\3$","111.11.111.111|443|0|11|0|orgaization.com\\2$","111.11.111.111|443|0|11|0|orgaization.com\\2$"]},
                    {"Name":"Acceptance  State","Values":["Success"]},
                    {"Name":"Validity Zone","Values":["120"]},
                    {"Name":"ClearMode","Values":["0"]},
                    {"Name":"Approval CA:Count","Values":["1"]},
                    {"Name":"Reader","Values":["Office"]},
                    {"Name":"End Date","Values":[""]},
                    {"Name":"Meeting Subject","Values":["adb.organization.com"]},
                    {"Name":"SubjectAltName DNS","Values":["adb.organization.com","ade.organization.com","Create.orgaization.com"]}],
"Result":1}

1 个答案:

答案 0 :(得分:0)

JSON中有两个问题行:

  • 此行的"放错了位置

{"Name":"Opened On","Values":["Mark, John, L=Toronto, S=Ontario, C=CA","www.doman.com", O=Office, L=Toronto, S=Ontario, C="CA"]},

应该是

{"Name":"Opened On","Values":["Mark, John, L=Toronto, S=Ontario, C=CA","www.doman.com", "O=Office, L=Toronto, S=Ontario, C=CA"]},

  • 这行

{"Name":"Internet System Address","Values"["www.doman.com/Locations"]},

“值”后应有一个:

{"Name":"Internet System Address","Values":["www.doman.com/Locations"]},

解决了这些问题后,结果就是有效的JSON,可以将其通过管道传递到ConvertFrom-JSON:

$JSON = @"
{"ResultForName":
    [{"Name":"work","Values":["no"]},
    {"Name":"location","Values":["\\pc\\documents"]},
    {"Name":"Authority","Values":["10"]},
    {"Name":"City","Values":["Toronto"]},
    {"Name":"Country","Values":["CA"]},
    {"Name":"Created By","Values":["George"]},
    {"Name":"Money Vault Id","Values":["100000"]},
    {"Name":"Opened  On","Values":["Mark, John, L=Toronto, S=Ontario, C=CA","www.doman.com", "O=Office, L=Toronto, S=Ontario, C=CA"]},
    {"Name":"Driver Name","Values":["Honda"]},
    {"Name":"Can drive","Values":["0"]},
    {"Name":"Given Name","Values":["Steven"]},
    {"Name":"Group Id","Values":["34324"]},
    {"Name":"Internet System Address","Values":["www.doman.com/Locations"]},
    {"Name":"Key value","Values":["AXU"]},
    {"Name":"Key Encryption","Values":["128"]},
    {"Name":"Last Check up","Values":["12\/20\/2018 04:00:08"]},
    {"Name":"Last Notification","Values":["11\/22\/2018 05:00:59"]},
    {"Name":"Last used by","Values":["David"]},
    {"Name":"Last Used On","Values":["09\/11\/2018 14:57:50"]},
    {"Name":"Validation","Values":["02\/22\/2019 04:06:18"]},
    {"Name":"Last Validation query","Values":["Success"]},
    {"Name":"Last Acceptence","Values":["04\/12\/2019 04:10:11"]},
    {"Name":"Managed By","Values":["Manager"]},
    {"Name":"Automatic","Values":["True"]},
    {"Name":"Options","Values":["More options Needed"]},
    {"Name":"Organization","Values":["Organization"]},
    {"Name":"Organizational Unit","Values":["CSV"]},
    {"Name":"Work Location Date","Values":["02\/29\/2019 02:00:40"]},
    {"Name":"URL Details","Values":["CN=web.website.com,O=\"Organization\",OU=CSV,L=Toronto,ST=Ontario,C=CA"]},
    {"Name":"Provence","Values":["Ontario"]},
    {"Name":"Surname","Values":["Johnson"]},
    {"Name":"Transaction value","Values":["23452523"]},
    {"Name":"Validation Acceptance Values",
                    "Values":["111.11.11.11|443|0|11|0|organization.com\\3$","111.11.111.111|443|0|11|0|orgaization.com\\2$","111.11.111.111|443|0|11|0|orgaization.com\\2$"]},
    {"Name":"Acceptance  State","Values":["Success"]},
    {"Name":"Validity Zone","Values":["120"]},
    {"Name":"ClearMode","Values":["0"]},
    {"Name":"Approval CA:Count","Values":["1"]},
    {"Name":"Reader","Values":["Office"]},
    {"Name":"End Date","Values":[""]},
    {"Name":"Meeting Subject","Values":["adb.organization.com"]},
    {"Name":"SubjectAltName DNS","Values":["adb.organization.com","ade.organization.com","Create.orgaization.com"]}],
"Result":1}
"@
$JSON | ConvertFrom-Json