尽管我的对象是合法的Json,但是ConvertFrom-Json无法成功地将我的Json解析为对象。
我正在使用最新版本的Powershell。
当我尝试解析像这样的谷歌请求JSON主体时:
{
"reportScope": {
"agencyId": "11111111111111111",
"advertiserId": "22222222222222222"
},
"reportType": "adGroup",
"columns": [
{
"columnName": "status"
},
{
"columnName": "date"
},
{
"columnName": "account"
},
{
"columnName": "accountType"
},
{
"columnName": "campaign"
},
{
"columnName": "adGroup"
},
{
"columnName": "deviceSegment"
},
{
"columnName": "impr"
},
{
"columnName": "clicks"
},
{
"columnName": "dfaRevenue"
},
{
"columnName": "cost"
},
{
"columnName": "avgPos"
}
],
"timeRange": {
"startDate": "2017-09-13",
"endDate": "2017-08-14"
},
"filters": [
{
"column": {"columnName": "impr"},
"operator": "greaterThan",
"values": "0"
}
],
"downloadFormat": "csv",
"maxRowsPerFile": 6000000,
"statisticsCurrency": "agency",
"verifySingleTimeZone": "false",
"includeRemovedEntities": "false"
}
我得到一个这样的对象:
[DBG]: PS Microsoft.PowerShell.Core\FileSystem::\\psDashboard>> $objJson
reportScope : @{agencyId=11111111111111111; advertiserId=22222222222222222}
reportType : adGroup
columns : {@{columnName=status}, @{columnName=date}, @{columnName=account}, @{columnName=accountType}...}
timeRange : @{startDate=2017-09-13; endDate=2017-08-14}
filters : {@{column=; operator=greaterThan; values=0}}
downloadFormat : csv
maxRowsPerFile : 6000000
statisticsCurrency : agency
verifySingleTimeZone : false
includeRemovedEntities : false
[DBG]: PS Microsoft.PowerShell.Core\FileSystem::\\psDashboard>> $objJson.filters
column operator values
------ -------- ------
greaterThan 0
注意column
应该包含@{columnNane=imgr}
,但它不包含任何内容。
用于解析的代码:
$objJson = Get-Content -Path $FileName -Raw | ConvertFrom-Json
有什么想法吗?
答案 0 :(得分:1)
我拿了你的代码并且能够找到你正在寻找的对象我首先把它放在Here字符串@“”@并且能够到达它...然后就像你把它放在一个变量中管道转换为-json并得到了结果。
$test = @"
{
"reportScope": {
"agencyId": "11111111111111111",
"advertiserId": "22222222222222222"
},
"reportType": "adGroup",
"columns": [
{
"columnName": "status"
},
{
"columnName": "date"
},
{
"columnName": "account"
},
{
"columnName": "accountType"
},
{
"columnName": "campaign"
},
{
"columnName": "adGroup"
},
{
"columnName": "deviceSegment"
},
{
"columnName": "impr"
},
{
"columnName": "clicks"
},
{
"columnName": "dfaRevenue"
},
{
"columnName": "cost"
},
{
"columnName": "avgPos"
}
],
"timeRange": {
"startDate": "2017-09-13",
"endDate": "2017-08-14"
},
"filters": [
{
"column": {"columnName": "impr"},
"operator": "greaterThan",
"values": "0"
}
],
"downloadFormat": "csv",
"maxRowsPerFile": 6000000,
"statisticsCurrency": "agency",
"verifySingleTimeZone": "false",
"includeRemovedEntities": "false"
}
"@
$t = $test | ConvertFrom-Json
$t.filters.column
PS C:\WINDOWS\system32> $t.filters
column operator values
------ -------- ------
@{columnName=impr} greaterThan 0
PS C:\WINDOWS\system32> $t.filters.column
columnName
----------
impr
使用获取内容并使用convertfrom-json。
PS C:\WINDOWS\system32> $test.filters
column operator values
------ -------- ------
@{columnName=impr} greaterThan 0
PS C:\WINDOWS\system32> $test.filters.column
columnName
----------
impr
可能是你的版本的convertFrom-json我的模块版本是:
PS C:\ WINDOWS \ system32> gcm convertfrom-json
CommandType名称版本来源
----------- ---- ------- ------
Cmdlet ConvertFrom-Json 3.1.0.0 Microsoft.PowerShell.Utility