4

JSONオブジェクトのPythonで特定の値を取得しようとしています。次のようなものを使用する前に:

data ['data'] ['data2']

data2に関連付けられている特定の値を取得するために、これは少し異なります。私のJSONファイルはより複雑になり、これは

{
    "data": {
        "playerStatSummaries": {
            "playerStatSummarySet": [
                {
                    "aggregatedStats": {
                        "stats": []
                    }, 
                    "leaves": 0, 
                    "losses": 0, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1357567398182)/", 
                    "playerStatSummaryType": "Unranked3x3", 
                    "rating": 400, 
                    "wins": 5
                }, 
                {
                    "aggregatedStats": {
                        "stats": []
                    }, 
                    "leaves": 0, 
                    "losses": 0, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1357567398182)/", 
                    "playerStatSummaryType": "AramUnranked6x6", 
                    "rating": 400, 
                    "wins": 0
                }, 
                {
                    "aggregatedStats": {
                        "stats": [
                            {
                                "statType": "TOTAL_CHAMPION_KILLS", 
                                "value": 68
                            }, 
                            {
                                "statType": "TOTAL_ASSISTS", 
                                "value": 116
                            }, 
                            {
                                "statType": "TOTAL_MINION_KILLS", 
                                "value": 1854
                            }, 
                            {
                                "statType": "TOTAL_TURRETS_KILLED", 
                                "value": 22
                            }, 
                            {
                                "statType": "TOTAL_NEUTRAL_MINIONS_KILLED", 
                                "value": 359
                            }
                        ]
                    }, 
                    "leaves": 0, 
                    "losses": 5, 
                    "maxRating": 1505, 
                    "modifyDate": "/Date(1357261303440)/", 
                    "playerStatSummaryType": "RankedSolo5x5", 
                    "rating": 1505, 
                    "wins": 9
                }, 
                {
                    "aggregatedStats": {
                        "stats": [
                            {
                                "statType": "TOTAL_CHAMPION_KILLS", 
                                "value": 369
                            }, 
                            {
                                "statType": "TOTAL_ASSISTS", 
                                "value": 535
                            }, 
                            {
                                "statType": "TOTAL_MINION_KILLS", 
                                "value": 9917
                            }, 
                            {
                                "statType": "TOTAL_TURRETS_KILLED", 
                                "value": 78
                            }, 
                            {
                                "statType": "TOTAL_NEUTRAL_MINIONS_KILLED", 
                                "value": 1050
                            }
                        ]
                    }, 
                    "leaves": 0, 
                    "losses": 35, 
                    "maxRating": 1266, 
                    "modifyDate": "/Date(1323496849000)/", 
                    "playerStatSummaryType": "RankedTeam5x5", 
                    "rating": 1266, 
                    "wins": 39
                }, 
                {
                    "aggregatedStats": {
                        "stats": [
                            {
                                "statType": "TOTAL_CHAMPION_KILLS", 
                                "value": 29
                            }, 
                            {
                                "statType": "TOTAL_ASSISTS", 
                                "value": 17
                            }, 
                            {
                                "statType": "TOTAL_MINION_KILLS", 
                                "value": 176
                            }, 
                            {
                                "statType": "TOTAL_TURRETS_KILLED", 
                                "value": 8
                            }, 
                            {
                                "statType": "TOTAL_NEUTRAL_MINIONS_KILLED", 
                                "value": 12
                            }
                        ]
                    }, 
                    "leaves": 0, 
                    "losses": 0, 
                    "maxRating": 1200, 
                    "modifyDate": "/Date(1326521499000)/", 
                    "playerStatSummaryType": "CoopVsAI", 
                    "rating": 1200, 
                    "wins": 2
                }, 
                {
                    "aggregatedStats": {
                        "stats": [
                            {
                                "statType": "TOTAL_CHAMPION_KILLS", 
                                "value": 150
                            }, 
                            {
                                "statType": "TOTAL_ASSISTS", 
                                "value": 184
                            }, 
                            {
                                "statType": "TOTAL_MINION_KILLS", 
                                "value": 3549
                            }, 
                            {
                                "statType": "TOTAL_TURRETS_KILLED", 
                                "value": 24
                            }, 
                            {
                                "statType": "TOTAL_NEUTRAL_MINIONS_KILLED", 
                                "value": 224
                            }
                        ]
                    }, 
                    "leaves": 0, 
                    "losses": 17, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1350098520000)/", 
                    "playerStatSummaryType": "RankedTeam3x3", 
                    "rating": 1308, 
                    "wins": 22
                }, 
                {
                    "aggregatedStats": {
                        "stats": [
                            {
                                "statType": "TOTAL_CHAMPION_KILLS", 
                                "value": 15
                            }, 
                            {
                                "statType": "TOTAL_ASSISTS", 
                                "value": 185
                            }, 
                            {
                                "statType": "TOTAL_MINION_KILLS", 
                                "value": 250
                            }, 
                            {
                                "statType": "TOTAL_TURRETS_KILLED", 
                                "value": 4
                            }, 
                            {
                                "statType": "TOTAL_NEUTRAL_MINIONS_KILLED", 
                                "value": 15
                            }
                        ]
                    }, 
                    "leaves": 0, 
                    "losses": 3, 
                    "maxRating": 1365, 
                    "modifyDate": "/Date(1321778545000)/", 
                    "playerStatSummaryType": "RankedPremade5x5", 
                    "rating": 1365, 
                    "wins": 8
                }, 
                {
                    "aggregatedStats": {
                        "stats": [
                            {
                                "statType": "TOTAL_CHAMPION_KILLS", 
                                "value": 672
                            }, 
                            {
                                "statType": "AVERAGE_CHAMPIONS_KILLED", 
                                "value": 9
                            }, 
                            {
                                "statType": "MAX_COMBAT_PLAYER_SCORE", 
                                "value": 889
                            }, 
                            {
                                "statType": "AVERAGE_OBJECTIVE_PLAYER_SCORE", 
                                "value": 771
                            }, 
                            {
                                "statType": "MAX_TEAM_OBJECTIVE", 
                                "value": 2
                            }, 
                            {
                                "statType": "MAX_NODE_CAPTURE", 
                                "value": 14
                            }, 
                            {
                                "statType": "MAX_OBJECTIVE_PLAYER_SCORE", 
                                "value": 1424
                            }, 
                            {
                                "statType": "MAX_TOTAL_PLAYER_SCORE", 
                                "value": 1950
                            }, 
                            {
                                "statType": "AVERAGE_NUM_DEATHS", 
                                "value": 10
                            }, 
                            {
                                "statType": "TOTAL_DECAYER", 
                                "value": 105
                            }, 
                            {
                                "statType": "TOTAL_ASSISTS", 
                                "value": 931
                            }, 
                            {
                                "statType": "AVERAGE_NODE_NEUTRALIZE", 
                                "value": 6
                            }, 
                            {
                                "statType": "AVERAGE_NODE_CAPTURE_ASSIST", 
                                "value": 2
                            }, 
                            {
                                "statType": "MAX_NODE_CAPTURE_ASSIST", 
                                "value": 5
                            }, 
                            {
                                "statType": "MAX_ASSISTS", 
                                "value": 25
                            }, 
                            {
                                "statType": "AVERAGE_NODE_NEUTRALIZE_ASSIST", 
                                "value": 1
                            }, 
                            {
                                "statType": "AVERAGE_TOTAL_PLAYER_SCORE", 
                                "value": 1182
                            }, 
                            {
                                "statType": "MAX_NODE_NEUTRALIZE_ASSIST", 
                                "value": 3
                            }, 
                            {
                                "statType": "AVERAGE_COMBAT_PLAYER_SCORE", 
                                "value": 413
                            }, 
                            {
                                "statType": "AVERAGE_NODE_CAPTURE", 
                                "value": 8
                            }, 
                            {
                                "statType": "MAX_CHAMPIONS_KILLED", 
                                "value": 20
                            }, 
                            {
                                "statType": "TOTAL_NODE_NEUTRALIZE", 
                                "value": 391
                            }, 
                            {
                                "statType": "AVERAGE_TEAM_OBJECTIVE", 
                                "value": 1
                            }, 
                            {
                                "statType": "AVERAGE_ASSISTS", 
                                "value": 11
                            }, 
                            {
                                "statType": "TOTAL_NODE_CAPTURE", 
                                "value": 447
                            }, 
                            {
                                "statType": "MAX_NODE_NEUTRALIZE", 
                                "value": 11
                            }, 
                            {
                                "statType": "MAX_NUM_DEATHS", 
                                "value": 16
                            }
                        ]
                    }, 
                    "leaves": 0, 
                    "losses": 0, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1357567398182)/", 
                    "playerStatSummaryType": "OdinUnranked", 
                    "rating": 400, 
                    "wins": 43
                }, 
                {
                    "aggregatedStats": {
                        "stats": []
                    }, 
                    "leaves": 0, 
                    "losses": 0, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1357567398182)/", 
                    "playerStatSummaryType": "AramUnranked2x2", 
                    "rating": 400, 
                    "wins": 0
                }, 
                {
                    "aggregatedStats": {
                        "stats": []
                    }, 
                    "leaves": 0, 
                    "losses": 0, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1357567398182)/", 
                    "playerStatSummaryType": "AramUnranked1x1", 
                    "rating": 400, 
                    "wins": 0
                }, 
                {
                    "aggregatedStats": {
                        "stats": []
                    }, 
                    "leaves": 0, 
                    "losses": 0, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1357567398182)/", 
                    "playerStatSummaryType": "AramUnranked3x3", 
                    "rating": 400, 
                    "wins": 0
                }, 
                {
                    "aggregatedStats": {
                        "stats": [
                            {
                                "statType": "TOTAL_CHAMPION_KILLS", 
                                "value": 10269
                            }, 
                            {
                                "statType": "TOTAL_DECAYER", 
                                "value": 0
                            }, 
                            {
                                "statType": "TOTAL_ASSISTS", 
                                "value": 15722
                            }, 
                            {
                                "statType": "TOTAL_MINION_KILLS", 
                                "value": 262793
                            }, 
                            {
                                "statType": "TOTAL_TURRETS_KILLED", 
                                "value": 1954
                            }, 
                            {
                                "statType": "TOTAL_NEUTRAL_MINIONS_KILLED", 
                                "value": 43898
                            }, 
                            {
                                "statType": "TOTAL_DEATHS_PER_SESSION", 
                                "value": 1513
                            }
                        ]
                    }, 
                    "leaves": 1, 
                    "losses": 0, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1357567398182)/", 
                    "playerStatSummaryType": "Unranked", 
                    "rating": 400, 
                    "wins": 1691
                }, 
                {
                    "aggregatedStats": {
                        "stats": []
                    }, 
                    "leaves": 0, 
                    "losses": 0, 
                    "maxRating": 0, 
                    "modifyDate": "/Date(1357567398182)/", 
                    "playerStatSummaryType": "AramUnranked5x5", 
                    "rating": 400, 
                    "wins": 0
                }
            ]
        }, 
        "previousFirstWinOfDay": "/Date(1357489166306)/", 
        "userId": 55060
    }, 
    "success": true
}

ご覧のとおり、これは非常に長いので、私の質問は、特定のplayerStatSummarySetセットから特定の値のみを取得するにはどうすればよいかということです。たとえば、セットから評価値を取得したいのは、どのように行うのかというplayerStatSummaryType値だけだとしましょう。RankedSolo5x5

これが、JSONファイルからデータを取得するためにこれまで行ってきたことです。

with open('data.txt', 'r') as f:
    data = json.load(f)
4

3 に答える 3

4

複雑なjsonオブジェクトを操作する必要がある場合は、オブジェクトに同様の言語を提供するjsonpathを確認することをお勧めします。xpathjson

例:

import jsonpath
import json
with open('/test.json', 'r') as f:
    data = json.load(f)
path = "$.[?(@.playerStatSummaryType == 'RankedSolo5x5')].rating"
jsonpath.jsonpath(data,path)

アウト:

[1505]
于 2013-01-07T15:24:20.067 に答える
1

リスト内包表記を使用する

with open('data.txt', 'r') as f:
    data = json.load(f)
rating = [summary["rating"] for summary
          in data["data"]["playerStatSummaries"]["playerStatSummarySet"]
          if summary["playerStatSummaryType"] == "RankedSolo5x5"][0]
于 2013-01-07T15:26:59.433 に答える
-1

それでも実行できますが、データ構造に適切にアクセスする必要があります。json.load()返されるのはPython辞書とJSON object同じです。このobjには、'data'別のオブジェクトディクショナリなどに関連付けられた名前のキーがあり、キーが設定され'playerStatSummaries'たデータメンバーを持つオブジェクトに到達するまで、'playerStatSummarySet'実際には別のオブジェクトディクショナリではなくPythonリストになります。

サマリーセットのリストを検索して特定のエントリを見つける方法の例を次に示します。このデータ項目は辞書オブジェクトではなくリストであるため、各エントリをステップスルーして自分のエントリを見つける必要があることに注意してください。その名前を調べるだけでなく、探しています。

import json

with open('data.txt', 'r') as f:
    jsonObj = json.load(f)

targetSummaryType = 'RankedSolo5x5'
for summarySet in jsonObj['data']['playerStatSummaries']['playerStatSummarySet']:
    if summarySet['playerStatSummaryType'] == targetSummaryType:
        print 'max rating for {}: {}'.format(targetSummaryType,
                                             summarySet['maxRating'])
        break  # if you only expect there to be one

出力:

max rating for RankedSolo5x5: 1505

何が必要かを理解するために、最初pprint()は全体を理解することが有用であることがわかりましjsonObjた。これにより、構造が非常に見やすくなりました。

于 2013-01-07T17:10:40.867 に答える