I have a problem regarding my python code that needs to create a multiline JSON type of data.
我的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))
Currently, it is showing these data
目前,它正在显示这些数据
{
"dashboard":
{"bounces": "12", "userType": "Returning Visitor", "users": "3"}
}
But what I want the output is to display this kind of data
但我想要的输出是显示这种数据
{
"dashboard":
{"bounces": "10", "userType": "New Visitor", "users": "15"},
{"bounces": "12", "userType": "Returning Visitor", "users": "3"}
}
3 个解决方案
#1
1
Your output data example is not a valid json, nevertheless this would fix it:
您的输出数据示例不是有效的json,但这会解决它:
def get_json_data(response):
x ={}
f ={'dashboad' : []}
...
f['dashboard'].append(x)
print (json.dumps(f))
print (json.dumps(f))
The dashboard value should be a list so then you can append the different x
results
仪表板值应该是一个列表,这样您就可以附加不同的x结果
#2
2
you format should be like this
你的格式应该是这样的
df1 = {
"dashboard":
[{"bounces": "10", "userType": "New Visitor", "users": "15"},
{"bounces": "12", "userType": "Returning Visitor", "users": "3"}]
}
otherwise it will give error
否则会出错
#3
0
You cannot build this kind of structure because python does not allow this. Just type this in your terminal you'll get idea about structure error
你无法构建这种结构,因为python不允许这样做。只需在您的终端中键入此内容,您就可以了解结构错误
{
"dashboard":
{"bounces": "10", "userType": "New Visitor", "users": "15"},
{"bounces": "12", "userType": "Returning Visitor", "users": "3"}
}
#1
1
Your output data example is not a valid json, nevertheless this would fix it:
您的输出数据示例不是有效的json,但这会解决它:
def get_json_data(response):
x ={}
f ={'dashboad' : []}
...
f['dashboard'].append(x)
print (json.dumps(f))
print (json.dumps(f))
The dashboard value should be a list so then you can append the different x
results
仪表板值应该是一个列表,这样您就可以附加不同的x结果
#2
2
you format should be like this
你的格式应该是这样的
df1 = {
"dashboard":
[{"bounces": "10", "userType": "New Visitor", "users": "15"},
{"bounces": "12", "userType": "Returning Visitor", "users": "3"}]
}
otherwise it will give error
否则会出错
#3
0
You cannot build this kind of structure because python does not allow this. Just type this in your terminal you'll get idea about structure error
你无法构建这种结构,因为python不允许这样做。只需在您的终端中键入此内容,您就可以了解结构错误
{
"dashboard":
{"bounces": "10", "userType": "New Visitor", "users": "15"},
{"bounces": "12", "userType": "Returning Visitor", "users": "3"}
}