如何使用 python 回圈一個看起來像下面的 json 陣列?{
"insights": {
"data": [
{
"name": "page_impressions",
"period": "day",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 17,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "Daily Total Impressions",
"description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/day"
},
{
"name": "page_impressions",
"period": "week",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 31,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "Weekly Total Impressions",
"description": "Weekly: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/week"
},
{
"name": "page_impressions",
"period": "days_28",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 31,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "28 Days Total Impressions",
"description": "28 Days: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/days_28"
}
]
我知道如何遍歷單個專案:
values = profile['insights']['data'][0]['values'][0]
但考慮到我需要遍歷每個專案并顯示輸出并存盤它,這不是一個可行的解決方案。任何幫助,將不勝感激。
uj5u.com熱心網友回復:
如何迭代 json,這是您可以做到的一種方法:
資料:
test = {
"insights": {
"data": [
{
"name": "page_impressions",
"period": "day",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 17,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "Daily Total Impressions",
"description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/day"
},
{
"name": "page_impressions",
"period": "week",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 31,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "Weekly Total Impressions",
"description": "Weekly: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/week"
},
{
"name": "page_impressions",
"period": "days_28",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 31,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "28 Days Total Impressions",
"description": "28 Days: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/days_28"
}
]
}
}
代碼:
for data in test["insights"]["data"]:
print(data["title"])
for value in data["values"]:
print(value)
結果:
Daily Total Impressions
{'value': 14, 'end_time': '2022-05-16T07:00:00 0000'}
{'value': 17, 'end_time': '2022-05-17T07:00:00 0000'}
Weekly Total Impressions
{'value': 14, 'end_time': '2022-05-16T07:00:00 0000'}
{'value': 31, 'end_time': '2022-05-17T07:00:00 0000'}
28 Days Total Impressions
{'value': 14, 'end_time': '2022-05-16T07:00:00 0000'}
{'value': 31, 'end_time': '2022-05-17T07:00:00 0000'}
如果您需要進一步解壓:
for data in test["insights"]["data"]:
print(data["title"])
for val in data["values"]:
for key, value in val.items():
print(f"The value: [ {value} ]")
結果:
Daily Total Impressions
The value: [ 14 ]
The value: [ 2022-05-16T07:00:00 0000 ]
The value: [ 17 ]
The value: [ 2022-05-17T07:00:00 0000 ]
Weekly Total Impressions
The value: [ 14 ]
The value: [ 2022-05-16T07:00:00 0000 ]
The value: [ 31 ]
The value: [ 2022-05-17T07:00:00 0000 ]
28 Days Total Impressions
The value: [ 14 ]
The value: [ 2022-05-16T07:00:00 0000 ]
The value: [ 31 ]
The value: [ 2022-05-17T07:00:00 0000 ]
uj5u.com熱心網友回復:
profile = {
"insights": {
"data": [
{
"name": "page_impressions",
"period": "day",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 17,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "Daily Total Impressions",
"description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/day"
},
{
"name": "page_impressions",
"period": "week",
"values": [
{
"value": 15,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 31,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "Weekly Total Impressions",
"description": "Weekly: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/week"
},
{
"name": "page_impressions",
"period": "days_28",
"values": [
{
"value": 16,
"end_time": "2022-05-16T07:00:00 0000"
},
{
"value": 33,
"end_time": "2022-05-17T07:00:00 0000"
}
],
"title": "28 Days Total Impressions",
"description": "28 Days: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/days_28"
}
]}}
有兩種方法可以做到:
選項 (1) 固定架構
result = []
for i in range(3):
temp = []
for j in range(2):
temp.append(profile['insights']['data'][i]['values'][j])
result.append(temp)
print(result)
任何模式的選項 (2)
def get_vals(nested, key):
result = []
if isinstance(nested, list) and nested != []: #non-empty list
for lis in nested:
result.extend(get_vals(lis, key))
elif isinstance(nested, dict) and nested != {}: #non-empty dict
for val in nested.values():
if isinstance(val, (list, dict)): #(list or dict) in dict
result.extend(get_vals(val, key))
if key in nested.keys(): #key found in dict
result.append(nested[key])
return result
result = get_vals(profile, 'values')
print(result)
兩個選項都將輸出:
[[{'value': 14, 'end_time': '2022-05-16T07:00:00 0000'},
{'value': 17, 'end_time': '2022-05-17T07:00:00 0000'}],
[{'value': 15, 'end_time': '2022-05-16T07:00:00 0000'},
{'value': 31, 'end_time': '2022-05-17T07:00:00 0000'}],
[{'value': 16, 'end_time': '2022-05-16T07:00:00 0000'},
{'value': 33, 'end_time': '2022-05-17T07:00:00 0000'}]]
之后,您可以將它們放在 DataFrame 中
import pandas as pd
df = pd.DataFrame(result).T
df.columns = ['Daily Total Impressions', 'Weekly Total Impressions', '28 Days Total Impressions']
輸出
Daily Total Impressions \
0 {'value': 14, 'end_time': '2022-05-16T07:00:00...
1 {'value': 17, 'end_time': '2022-05-17T07:00:00...
Weekly Total Impressions \
0 {'value': 15, 'end_time': '2022-05-16T07:00:00...
1 {'value': 31, 'end_time': '2022-05-17T07:00:00...
28 Days Total Impressions
0 {'value': 16, 'end_time': '2022-05-16T07:00:00...
1 {'value': 33, 'end_time': '2022-05-17T07:00:00...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/478438.html
標籤:Python
下一篇:從csv檔案中的多行中提取唯一值
