2017-03-31 45 views
0

我想从下面的json数据框中提取一些数据,使用Python 2.7.9。提取和格式从嵌套的JSON数据的值

我想用下面的代码去其内部的“台阶”列在一排的计数列表:

data2 = (json.loads(data)); 

for data_key,data_value in data2['data'].items(): 
for date_key,date_value in data_value.items(): 
    for steps_value in date_value: 
     if 'steps' == date_key: #looping on dictionary object 
      print data_key,steps_value['count'],steps_value['event'],steps_value['custom_event'] 

在进一步深挖,我设法取得一些进展,并获得价值。我不是一个Python专家,所以想知道这是否可以以更优雅的方式完成。想找到如下的输出。

理想输出格式:
日期1步骤1步骤2共1个记录共2个记录
日期2步骤2共1个记录步骤2共2个记录

示例数据帧:

"frame": 
{"dates": ["2016-09-12", "2016-09-19", "2016-09-26"]}, 
"data": { 
    "2016-09-12": 
     {"steps": [ 
      {"count": 325788, "step_conv_ratio": 1, "goal": "App Open", "overall_conv_ratio": 1, "avg_time": null, "event": "App Open"}, 
      {"count": 20524, "step_conv_ratio": 0.627875673029858, "goal": "Game Played", 
      "avg_time": 572, "event": "Game Played"}], 
     "analysis": {"completion": 20524, "starting_amount": 32688, "steps": 2, "worst": 1}}, 
    "2016-09-19": 
     {"steps": [ 
      {"count": 32186, "step_conv_ratio": 1, "goal": "App Open", "overall_conv_ratio": 1, "avg_time": null, "event": "App Open"}, 
      {"count": 20809, "step_conv_ratio": 0.6405528535369082, "goal": "Game Played", 
      "avg_time": 698, "event": "Game Played"}], 
     "analysis": {"completion": 20809, "starting_amount": 32486, "steps": 2, "worst": 1}}, 
    "2016-09-26": 
     {"steps": [ 
      {"count": 456, "step_conv_ratio": 1, "goal": "App Open", "overall_conv_ratio": 1, "avg_time": null, "event": "App Open"}, 
      {"count": 587, "step_conv_ratio": 0.7873688132646091, "goal": "Game Played", 
      "avg_time": 571, "event": "Game Played"}], 
     "analysis": {"completion": 12679, "starting_amount": 16103, "steps": 2, "worst": 1}} 
    }} 

任何帮助/建议将不胜感激。

感谢

+0

[解析嵌套的JSON有效载荷蟒蛇]的可能的复制(http://stackoverflow.com/questions/37200654/parsing-nested-json-payload-python) –

回答

0

如果每个日期只有两个步骤,你可以这样做:

stuff = json.loads(data) 
dates = stuff["frame"]["dates"] # list of dates as strings 
for date in dates: 
    steps = stuff["data"][date]["steps"] # list of dicts 
    print("{0}: step 1: {1}, step 2: {2}".format(date, steps[0]["count"], steps[1]["count"])) 
+0

感谢您的答复。我可以尝试一下。但日期的数量可能因请求的类型而异。目前,我只是试图通过其中的每一个来获取数据。 –