我在响应中解析json时遇到了一些问题。 我想将数据加载到MySQL数据库中。
像这样的json:{
"data":
[{"dimensions":
[{"name":"2018-05-01"},
{"name":"Переходы из поисковых систем","icon_id":"2","icon_type":"traffic-source","id":"organic"},
{"name":"Смартфоны","id":"mobile"},
{"name":"Google, результаты поиска","id":"google_search","favicon":"google.com"},
{"iso_name":"RU MOW","id":"213","name":"Москва"},
{"icon_id":"225","icon_type":"country","iso_name":"RU","id":"225","name":"Россия"},
{"name":null,"url":null,"favicon":null}],
"metrics":[13.0,38.0]}]
}
我想得到这样的结果:
MySQL header => ym:s:date // ym:s:lastTrafficSource // ym:s:deviceCategory//ym:s:lastSearchEngine // ym:s:regionCity // ym:s:regionCountry // ym:s:lastSearchPhrase // ym:s:visits // ym:s:pageviews
insert rows => 2018-05-01 // Переходы из поисковых систем // Смартфоны // Google, результаты поиска // Москва // Россия // null // 13.0 // 38.0
我的功能示例:
def get_json():
r = requests.get(url, params=params)
json_data = json.loads(r.text)
parse = json_data.get('data')
for rows in parse:
get_dimensions = rows.get('dimensions')
get_metrics = rows.get('metrics')
for names in get_dimensions:
get_names = names.get('name')
print(get_names)
无法理解如何制作维度和指标列表 并加载到MySQL
答案 0 :(得分:0)
解决:
def get_json():
r = requests.get(url, params=params)
json_data = json.loads(r.text)
parse = json_data.get('data')
for rows in parse:
get_dimensions = rows.get('dimensions')
date = get_dimensions[0].get('name')
source = get_dimensions[1].get('name')
device = get_dimensions[2].get('name')
source1 = get_dimensions[3].get('name')
city = get_dimensions[4].get('name')
country = get_dimensions[5].get('name')
search_phrase = get_dimensions[6].get('name')
row = []
row.append(date)
row.append(source)
row.append(device)
row.append(source1)
row.append(city)
row.append(country)
row.append(search_phrase)