用PowerShell处理复杂Json数据

之前有一篇文章我们提到PowerShell关于对JSON格式的一些转换,今天我们就要以实际角度来继续说下在PowerShell中操作JSON的数据格式了。

在我手中有一组,某网页游戏的JSON格式的数据如下:

{"sup1":124997,"serverId2":"86","name2":"老周","sup2":17770,"guanzhu1":true,"force1":2,"supSucNum":0,"force2":1,"matchId":12,"layerRound":3,"server1":"傲世堂","win1":1,"lv1":165,"server2":"野丁香","pic1":"4","win2":2,"lv2":165,"serverId1":"115","pic2":"5","name1":"老李"},
{"sup1":2078,"serverId2":"97","name2":"逍遥♂篮色神影","sup2":28092,"force1":1,"supSucNum":0,"force2":1,"matchId":6,"layerRound":3,"server1":"野丁香","win1":2,"lv1":165,"server2":"傲世堂","pic1":"5","win2":1,"lv2":165,"serverId1":"86","pic2":"3","name1":"老周"},
{"sup1":25664,"serverId2":"97","name2":"逍遥♂篮色神影","sup2":116904,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":1,"matchId":13,"layerRound":3,"server1":"人人游戏","win1":0,"lv1":165,"server2":"傲世堂","pic1":"2","win2":3,"lv2":165,"serverId1":"1","pic2":"3","name1":"淳"},
{"sup1":29091,"serverId2":"1","name2":"冠亚之军","sup2":1468,"force1":2,"supSucNum":0,"force2":2,"matchId":5,"layerRound":3,"server1":"凤凰网游戏","win1":0,"lv1":165,"server2":"妖豆游戏","pic1":"5","win2":3,"lv2":165,"serverId1":"13","pic2":"6","name1":"老顽童"},
{"sup1":1093,"serverId2":"1","name2":"诸神承诺的永远","sup2":28760,"force1":3,"supSucNum":0,"force2":1,"matchId":7,"layerRound":3,"server1":"凤凰网游戏","win1":3,"lv1":165,"server2":"起点","pic1":"5","win2":0,"lv2":165,"serverId1":"79","pic2":"5","name1":"乱打"},
{"sup1":1820,"serverId2":"53","name2":"李大叔","sup2":142439,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":1,"matchId":8,"layerRound":3,"server1":"野丁香","win1":0,"lv1":163,"server2":"野丁香","pic1":"4","win2":3,"lv2":165,"serverId1":"86","pic2":"5","name1":"刘凯123"},
{"sup1":21257,"serverId2":"180","name2":"大业","sup2":121249,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":2,"matchId":9,"layerRound":3,"server1":"傲世堂","win1":0,"lv1":165,"server2":"傲世堂","pic1":"5","win2":3,"lv2":165,"serverId1":"183","pic2":"2","name1":"凌丕"},
{"sup1":52805,"serverId2":"1","name2":"冠亚之军","sup2":231013,"force1":2,"supSucNum":1,"force2":2,"matchId":2,"layerRound":5,"server1":"傲世堂","win1":3,"lv1":165,"server2":"妖豆游戏","pic1":"2","win2":2,"lv2":165,"serverId1":"180","pic2":"6","name1":"大业"},
{"sup1":1584,"serverId2":"13","name2":"老顽童","sup2":142063,"force1":3,"supSucNum":1,"guanzhu2":true,"force2":2,"matchId":10,"layerRound":3,"server1":"唐门","win1":0,"lv1":165,"server2":"凤凰网游戏","pic1":"5","win2":3,"lv2":165,"serverId1":"37","pic2":"5","name1":"花大爷"}
这是很长的一串JSON格式的数据,里面有好几组数据,我的最终目的是想要把这串数据顺利的通过PowerShell中的JSON命令转换为PSObject对象,然后在进行一系列的对象处理。如下我试了几个转换JSON数据的方法:

$json = @"
{"sup1":124997,"serverId2":"86","name2":"老周","sup2":17770,"guanzhu1":true,"force1":2,"supSucNum":0,"force2":1,"matchId":12,"layerRound":3,"server1":"傲世堂","win1":1,"lv1":165,"server2":"野丁香","pic1":"4","win2":2,"lv2":165,"serverId1":"115","pic2":"5","name1":"老李"},
{"sup1":2078,"serverId2":"97","name2":"逍遥♂篮色神影","sup2":28092,"force1":1,"supSucNum":0,"force2":1,"matchId":6,"layerRound":3,"server1":"野丁香","win1":2,"lv1":165,"server2":"傲世堂","pic1":"5","win2":1,"lv2":165,"serverId1":"86","pic2":"3","name1":"老周"},
{"sup1":25664,"serverId2":"97","name2":"逍遥♂篮色神影","sup2":116904,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":1,"matchId":13,"layerRound":3,"server1":"人人游戏","win1":0,"lv1":165,"server2":"傲世堂","pic1":"2","win2":3,"lv2":165,"serverId1":"1","pic2":"3","name1":"淳"},
{"sup1":29091,"serverId2":"1","name2":"冠亚之军","sup2":1468,"force1":2,"supSucNum":0,"force2":2,"matchId":5,"layerRound":3,"server1":"凤凰网游戏","win1":0,"lv1":165,"server2":"妖豆游戏","pic1":"5","win2":3,"lv2":165,"serverId1":"13","pic2":"6","name1":"老顽童"},
{"sup1":1093,"serverId2":"1","name2":"诸神承诺的永远","sup2":28760,"force1":3,"supSucNum":0,"force2":1,"matchId":7,"layerRound":3,"server1":"凤凰网游戏","win1":3,"lv1":165,"server2":"起点","pic1":"5","win2":0,"lv2":165,"serverId1":"79","pic2":"5","name1":"乱打"},
{"sup1":1820,"serverId2":"53","name2":"李大叔","sup2":142439,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":1,"matchId":8,"layerRound":3,"server1":"野丁香","win1":0,"lv1":163,"server2":"野丁香","pic1":"4","win2":3,"lv2":165,"serverId1":"86","pic2":"5","name1":"刘凯123"},
{"sup1":21257,"serverId2":"180","name2":"大业","sup2":121249,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":2,"matchId":9,"layerRound":3,"server1":"傲世堂","win1":0,"lv1":165,"server2":"傲世堂","pic1":"5","win2":3,"lv2":165,"serverId1":"183","pic2":"2","name1":"凌丕"},
{"sup1":52805,"serverId2":"1","name2":"冠亚之军","sup2":231013,"force1":2,"supSucNum":1,"force2":2,"matchId":2,"layerRound":5,"server1":"傲世堂","win1":3,"lv1":165,"server2":"妖豆游戏","pic1":"2","win2":2,"lv2":165,"serverId1":"180","pic2":"6","name1":"大业"},
{"sup1":1584,"serverId2":"13","name2":"老顽童","sup2":142063,"force1":3,"supSucNum":1,"guanzhu2":true,"force2":2,"matchId":10,"layerRound":3,"server1":"唐门","win1":0,"lv1":165,"server2":"凤凰网游戏","pic1":"5","win2":3,"lv2":165,"serverId1":"37","pic2":"5","name1":"花大爷"}
"@

$json|ConvertFrom-Json
我把这些数据用Here-String方法保存了起来,并赋值给一个Json变量,在通过管道给ConvertFrom-Json命令,可是我却得到了一个报错的结果:

At line:13 char:7
+ $json|ConvertFrom-Json
+       ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
这个错误并不能给我什么帮助,只是很简单的说明转换异常,不能进行转换。那么问题出在哪里呢,冷静分析了一下,这些JSON数据时由一个个花括号并由逗号为分割组成的数据,而每个花括号里就时我们最常见的JSON数据格式,由此可以看出来,它更像是一个数组形式的数据,以花括号为一个数组元素,每个花括号由逗号","分隔,这与数组是类似的。所以我干脆把这串数据稍加修改,其实就是在最前端后最后端加上两个"[","]"对应的方括号,使它变成一个数组。当然由于里面的数组内容包含了引号等符号,为了使PowerShell能够顺利将它识别为数组,所以我们最后又用上了单引号,像如下这样:

$json = '[{"sup1":124997,"serverId2":"86","name2":"老周","sup2":17770,"guanzhu1":true,"force1":2,"supSucNum":0,"force2":1,"matchId":12,"layerRound":3,"server1":"傲世堂","win1":1,"lv1":165,"server2":"野丁香","pic1":"4","win2":2,"lv2":165,"serverId1":"115","pic2":"5","name1":"老李"},
{"sup1":2078,"serverId2":"97","name2":"逍遥♂篮色神影","sup2":28092,"force1":1,"supSucNum":0,"force2":1,"matchId":6,"layerRound":3,"server1":"野丁香","win1":2,"lv1":165,"server2":"傲世堂","pic1":"5","win2":1,"lv2":165,"serverId1":"86","pic2":"3","name1":"老周"},
{"sup1":25664,"serverId2":"97","name2":"逍遥♂篮色神影","sup2":116904,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":1,"matchId":13,"layerRound":3,"server1":"人人游戏","win1":0,"lv1":165,"server2":"傲世堂","pic1":"2","win2":3,"lv2":165,"serverId1":"1","pic2":"3","name1":"淳"},
{"sup1":29091,"serverId2":"1","name2":"冠亚之军","sup2":1468,"force1":2,"supSucNum":0,"force2":2,"matchId":5,"layerRound":3,"server1":"凤凰网游戏","win1":0,"lv1":165,"server2":"妖豆游戏","pic1":"5","win2":3,"lv2":165,"serverId1":"13","pic2":"6","name1":"老顽童"},
{"sup1":1093,"serverId2":"1","name2":"诸神承诺的永远","sup2":28760,"force1":3,"supSucNum":0,"force2":1,"matchId":7,"layerRound":3,"server1":"凤凰网游戏","win1":3,"lv1":165,"server2":"起点","pic1":"5","win2":0,"lv2":165,"serverId1":"79","pic2":"5","name1":"乱打"},
{"sup1":1820,"serverId2":"53","name2":"李大叔","sup2":142439,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":1,"matchId":8,"layerRound":3,"server1":"野丁香","win1":0,"lv1":163,"server2":"野丁香","pic1":"4","win2":3,"lv2":165,"serverId1":"86","pic2":"5","name1":"刘凯123"},
{"sup1":21257,"serverId2":"180","name2":"大业","sup2":121249,"force1":2,"supSucNum":1,"guanzhu2":true,"force2":2,"matchId":9,"layerRound":3,"server1":"傲世堂","win1":0,"lv1":165,"server2":"傲世堂","pic1":"5","win2":3,"lv2":165,"serverId1":"183","pic2":"2","name1":"凌丕"},
{"sup1":52805,"serverId2":"1","name2":"冠亚之军","sup2":231013,"force1":2,"supSucNum":1,"force2":2,"matchId":2,"layerRound":5,"server1":"傲世堂","win1":3,"lv1":165,"server2":"妖豆游戏","pic1":"2","win2":2,"lv2":165,"serverId1":"180","pic2":"6","name1":"大业"},
{"sup1":1584,"serverId2":"13","name2":"老顽童","sup2":142063,"force1":3,"supSucNum":1,"guanzhu2":true,"force2":2,"matchId":10,"layerRound":3,"server1":"唐门","win1":0,"lv1":165,"server2":"凤凰网游戏","pic1":"5","win2":3,"lv2":165,"serverId1":"37","pic2":"5","name1":"花大爷"}]'

$json | ConvertFrom-Json
运行之后,我们终于得到了熟悉的PowerShell自定义对象格式:

sup1       : 124997
serverId2  : 86
name2      : 老周
sup2       : 17770
guanzhu1   : True
force1     : 2
supSucNum  : 0
force2     : 1
matchId    : 12
layerRound : 3
server1    : 傲世堂
win1       : 1
lv1        : 165
server2    : 野丁香
pic1       : 4
win2       : 2
lv2        : 165
serverId1  : 115
pic2       : 5
name1      : 老李

sup1       : 2078
serverId2  : 97
name2      : 逍遥♂篮色神影
sup2       : 28092
force1     : 1
supSucNum  : 0
force2     : 1
matchId    : 6
layerRound : 3
server1    : 野丁香
win1       : 2
lv1        : 165
server2    : 傲世堂
pic1       : 5
win2       : 1
lv2        : 165
serverId1  : 86
pic2       : 3
name1      : 老周

sup1       : 25664
serverId2  : 97
name2      : 逍遥♂篮色神影
sup2       : 116904
force1     : 2
supSucNum  : 1
guanzhu2   : True
force2     : 1
matchId    : 13
layerRound : 3
server1    : 人人游戏
win1       : 0
lv1        : 165
server2    : 傲世堂
pic1       : 2
win2       : 3
lv2        : 165
serverId1  : 1
pic2       : 3
name1      : 淳

sup1       : 29091
serverId2  : 1
name2      : 冠亚之军
sup2       : 1468
force1     : 2
supSucNum  : 0
force2     : 2
matchId    : 5
layerRound : 3
server1    : 凤凰网游戏
win1       : 0
lv1        : 165
server2    : 妖豆游戏
pic1       : 5
win2       : 3
lv2        : 165
serverId1  : 13
pic2       : 6
name1      : 老顽童

sup1       : 1093
serverId2  : 1
name2      : 诸神承诺的永远
sup2       : 28760
force1     : 3
supSucNum  : 0
force2     : 1
matchId    : 7
layerRound : 3
server1    : 凤凰网游戏
win1       : 3
lv1        : 165
server2    : 起点
pic1       : 5
win2       : 0
lv2        : 165
serverId1  : 79
pic2       : 5
name1      : 乱打

sup1       : 1820
serverId2  : 53
name2      : 李大叔
sup2       : 142439
force1     : 2
supSucNum  : 1
guanzhu2   : True
force2     : 1
matchId    : 8
layerRound : 3
server1    : 野丁香
win1       : 0
lv1        : 163
server2    : 野丁香
pic1       : 4
win2       : 3
lv2        : 165
serverId1  : 86
pic2       : 5
name1      : 刘凯123
..........
.................
....................




你可能感兴趣的:(用PowerShell处理复杂Json数据)