我有一个代码,如果我执行此
$linksjson =$csv|select links
$linksjson
输出
links
-----
@{diff=@{href=https://api.bitbucket.org/2.0/repositories/diff/1234}; self=@{href=https://api.bitbucket.org/2.0/repositories/commit/1234}...
@{self=@{href=https://api.bitbucket.org/2.0/repositories/commit/1234}; comments=@{href=https://api.bitbucket.org/2.0/repositories/commit/1234...
@{self=@{href=https://api.bitbucket.org/2.0/repositories/commit/1234}; comments=@{href=https://api.bitbucket.org/2.0/repositories/commit/1234...
@{diff=@{href=https://api.bitbucket.org/2.0/repositories/diff/1234}; self=@{href=https://api.bitbucket.org/2.0/repositories/commit/1234}...
和其他有价值的
$parentsjson =$csv|select parents
$parentsjson
输出
{@{hash=de6eeb97; type=commit; links=}, @{hash=f6102; type=commit; links=}}
$ linksjson和$ parentsjson之间的差异是" @ {"或" {@ {" ,
对于$ linksjson,所以我尝试了[http://blogs.technet.com/b/heyscriptingguy/archive/2012/10/08/use-powershell-to-convert-to-or-from-json.aspx][1],经过Google搜索后,我认为这是json文件,所以我确实喜欢这个
$linksjson
$openlinks=$linksjson|convertFrom-json
$openlinks
但错误说
convertFrom-json : Invalid JSON primitive: .
对于$ parentsjson,如How can I show inside of System.Object[] Powershell, invoke-webrequest 我试过了
$parentsjson =$csv|select parents
$parentsjson.parents =$parentsjson.parents|ConvertTo-Json -Compress
$openparents=$parentsjson.parents|ConvertFrom-JSON
$openparents
错误是
Property 'parents' cannot be found on this object; make sure it exists and is settable.
如何修复此错误?我想看看$ openlinks的输出
diff:.....
self:.....
和$ openparents
hash:........
type:........
links:..........
重新编辑
**$csv is coming from**
$response = Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Uri https://api.bitbucket.org/2.0/repositories/helloworld/$NewArray/commits
$new = $response|select Values
$new.values= $new.values |ConvertTo-Json -Compress
$csv = $new.values| ConvertFrom-JSON
$csv
答案 0 :(得分:1)
看起来你的$ linksjson.links实际上包含哈希表,但是URL需要它们周围的引号才有用。这样的事情可能是一个开始的地方。
$link = '@{diff=@{href="https://api.bitbucket.org/2.0/repositories/diff/1234"};self=@{href="https://api.bitbucket.org/2.0/repositories/diff/1234"}}'
# this converts the hashtable string into a powershell hashtable object
Invoke-Expression $link
# and if you wanted, you could do this
Invoke-Expression $link | ConvertTo-Json