如何从JSON文件中打印值

时间:2018-10-29 23:41:44

标签: python json

{  
   "sessionid":"1",
   "game_clock_display":"1",
   "game_clock":1,
   "game_status":"score",
   "possession":[  
      1,
      1
   ],
   "teams":[  
      {  
         "players":[  
            {  
               "name":"NAME1",
               "playerid":1,
               "position":[  
                  64,
                  34,
                  45
               ],
               "stats":{  
                  "possession_time":21.882006,
                  "points":3,
                  "saves":1,
                  "goals":0,
                  "stuns":4,
                  "passes":0,
                  "catches":0,
                  "steals":1,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":2
               },
               "userid":xxx,
               "possession":false
            },
            {  
               "name":"NAME2",
               "playerid":3,
               "position":[  
                  -1.251,
                  2.8280001,
                  -24.380001
               ],
               "stats":{  
                  "possession_time":25.58037,
                  "points":2,
                  "saves":0,
                  "goals":0,
                  "stuns":4,
                  "passes":0,
                  "catches":0,
                  "steals":0,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":1
               },
               "userid":1583793368337226,
               "possession":false
            },
            {  
               "name":"NAME3",
               "playerid":5,
               "position":[  
                  0.89900005,
                  3.0540001,
                  -39.322002
               ],
               "stats":{  
                  "possession_time":28.325825,
                  "points":0,
                  "saves":1,
                  "goals":0,
                  "stuns":2,
                  "passes":0,
                  "catches":0,
                  "steals":0,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":2
               },
               "userid":1462448290539019,
               "possession":false
            }
         ],
         "team":"BLUE TEAM",
         "possession":false,
         "stats":{  
            "points":5,
            "possession_time":75.7882,
            "interceptions":0,
            "blocks":0,
            "steals":1,
            "catches":0,
            "passes":0,
            "saves":2,
            "goals":0,
            "stuns":10,
            "assists":0,
            "shots_taken":5
         }
      },
      {  
         "players":[  
            {  
               "name":"NAME4",
               "playerid":0,
               "position":[  
                  -0.058000002,
                  3.7800002,
                  3.9600003
               ],
               "stats":{  
                  "possession_time":15.404906,
                  "points":0,
                  "saves":2,
                  "goals":0,
                  "stuns":5,
                  "passes":0,
                  "catches":0,
                  "steals":1,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":1
               },
               "userid":2022757824404800,
               "possession":false
            },
            {  
               "name":"NAME5",
               "playerid":1,
               "position":[  
                  12.033001,
                  1.0680001,
                  -31.469002
               ],
               "stats":{  
                  "possession_time":29.619047,
                  "points":3,
                  "saves":0,
                  "goals":0,
                  "stuns":5,
                  "passes":0,
                  "catches":0,
                  "steals":0,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":2
               },
               "userid":1504614686325452,
               "possession":true
            },
            {  
               "name":"NAME6",
               "playerid":4,
               "position":[  
                  0.78100002,
                  1.9790001,
                  -17.815001
               ],
               "stats":{  
                  "possession_time":15.020118,
                  "points":2,
                  "saves":0,
                  "goals":0,
                  "stuns":2,
                  "passes":0,
                  "catches":0,
                  "steals":0,
                  "blocks":0,
                  "interceptions":0,
                  "assists":1,
                  "shots_taken":0
               },
               "userid":2207645752583213,
               "possession":false
            }
         ],
         "team":"ORANGE TEAM",
         "possession":true,
         "stats":{  
            "points":5,
            "possession_time":60.044071,
            "interceptions":0,
            "blocks":0,
            "steals":1,
            "catches":0,
            "passes":0,
            "saves":2,
            "goals":0,
            "stuns":12,
            "assists":1,
            "shots_taken":3
         }
      }
   ]
}

我的JSON文件,我想用“位置”和组(即蓝色或橙色组)打印“名称”的值。

所以我希望我在python中的最终输出是

名称:NAME1,位置:X,Y,Z团队:蓝色

名称:NAME2,位置:X,Y,Z团队:蓝色

名称:NAME3,位置:X,Y,Z团队:蓝色

名称:NAME4,位置:X,Y,Z团队:橙色

名称:NAME5,位置:X,Y,Z团队:橙色

名称:NAME6,位置:X,Y,Z团队:橙色

例如。

我将如何去做? python很新

2 个答案:

答案 0 :(得分:1)

您可以查看json模块:

import json

j = json.load(open("json-t.txt"))

for team in j['teams']:
    for p in team['players']:
        print(f"Name: {p['name']}, position: {p['position']}, team: {team['team']}") 

注意:在您留在"userid": xxx的示例json中,请确保此问题已解决。

答案 1 :(得分:0)

使用json模块,您可以将文件加载到标准Python字典中

import json

data = None

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

teams = data['teams']

现在您可以迭代teams提取所需的数据