我有一个关于Python代码的问题,需要创建多行JSON类型的数据。
def get_json_data(response):
x ={}
f ={}
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
for row in report.get('data', {}).get('rows', []):
dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])
for header, dimension in zip(dimensionHeaders, dimensions):
x[header.replace('la:','')] = dimension
for i, values in enumerate(dateRangeValues):
for metricHeader, value in zip(metricHeaders, values.get('values')):
x[metricHeader.get('name').replace('la:','')] = value
f['dashboard'] = x
print (json.dumps(f))
print (json.dumps(f))
目前,它正在显示这些数据
{
"dashboard":
{"bounces": "12", "userType": "Returning Visitor", "users": "3"}
}
但是我希望输出显示这种数据。
{
"dashboard":
{"bounces": "10", "userType": "New Visitor", "users": "15"},
{"bounces": "12", "userType": "Returning Visitor", "users": "3"}
}