PowerShellによる複雑なJsonデータの処理

16063 ワード

以前、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データフォーマットであることから、配列形式のデータのように、括弧を配列要素とし、括弧ごとにカンマ「,」で区切られていることがわかります.これは配列と似ています.だから私はいっそこのデータを少し修正して、実は最前端の後ろの最後の端に2つの「[」,「]」に対応する角カッコを加えて、それを1つの配列にします.もちろん中の配列には引用符などの記号が含まれているので、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
..........
.................
....................