Многоуровневый JSON python

307 просмотра
0
0 Комментариев

Имеется результат с json (VK API):

{
"response": {
"count": 1675,
"items": [{
"id": 1835,
"from_id": -156603484,
"owner_id": -156603484,
"date": 1517756029,
"marked_as_ads": 0,
"post_type": "post",
"text": "#artist_baekgup #cute #dashabetes #equestriagirls #fistbump #heart #hoofbump #humanponidox #rainbowdash #safe #selfponidox #squarecrossover",
"can_edit": 1,
"created_by": 213468131,
"can_delete": 1,
"can_pin": 1,
"attachments": [{
"type": "photo",
"photo": {
"id": 456240790,
"album_id": -7,
"owner_id": -156603484,
"user_id": 213468131,
"photo_75": "https://pp.userap...b09/4PbsucV0-4g.jpg",
"photo_130": "https://pp.userap...b0a/vHVTRcXyvyY.jpg",
"photo_604": "https://pp.userap...b0b/6cccdrLMecQ.jpg",
"width": 551,
"height": 500,
"text": "",
"date": 1517756029,
"post_id": 1835,
"access_key": "1be80adf605e9c36d8"
}
}],
"post_source": {
"type": "api"
},
"comments": {
"count": 0,
"groups_can_post": true,
"can_post": 1
},
"likes": {
"count": 1,
"user_likes": 0,
"can_like": 1,
"can_publish": 1
},
"reposts": {
"count": 0,
"user_reposted": 0
},
"views": {
"count": 2
}
}],
"profiles": [{
"id": 213468131,
"first_name": "Тимур",
"last_name": "Ивченко",
"sex": 2,
"screen_name": "jengas",
"photo_50": "https://pp.userap...328/iymw6YlJsRQ.jpg",
"photo_100": "https://pp.userap...327/J9eLMYmV_LI.jpg",
"online": 1
}],
"groups": [{
"id": 156603484,
"name": "Equestria in the Space",
"screen_name": "equestriaspace",
"is_closed": 0,
"type": "group",
"is_admin": 1,
"admin_level": 3,
"is_member": 1,
"photo_50": "https://pp.userap...089/u0_mBSE4E34.jpg",
"photo_100": "https://pp.userap...088/O6vENP0IW_w.jpg",
"photo_200": "https://pp.userap...086/rwntMz6YwWM.jpg"
}]
}
}

(Скриншотом виднее http://prntscr.com/i9z1i0)
И мне нужно получить "id": 1835, чтобы в консоли отображалось (1835)
Но код :

method_url = 'https://api.vk.com/method/wall.get?'
data = dict(access_token=access_token, gid=group_id)
response = requests.post(method_url, data)
results = json.loads(response.text)
postidd = results['response']['items'][0]['id']
print(postidd)

Но код не работает, пишет ошибку KeyError: 'response'


Добавить комментарий

1 Ответы

Python Опубликовано 09.12.2018
0

>>> import json
>>> m = '{"response":{"count":1675,"items":[{"id":1835,"from_id":-156603484,"owner_id":-156603484,"date":1517756029,"marked_as_ads":0,"post_type":"post","text":"#artist_baekgup #cute #dashabetes #equestriagirls #fistbump #heart #hoofbump #humanponidox #rainbowdash #safe #selfponidox #squarecrossover","can_edit":1,"created_by":213468131,"can_delete":1,"can_pin":1,"attachments":[{"type":"photo","photo":{"id":456240790,"album_id":-7,"owner_id":-156603484,"user_id":213468131,"photo_75":"https://pp.userap...b09/4PbsucV0-4g.jpg","photo_130":"https://pp.userap...b0a/vHVTRcXyvyY.jpg","photo_604":"https://pp.userap...b0b/6cccdrLMecQ.jpg","width":551,"height":500,"text":"","date":1517756029,"post_id":1835,"access_key":"1be80adf605e9c36d8"}}],"post_source":{"type":"api"},"comments":{"count":0,"groups_can_post":true,"can_post":1},"likes":{"count":1,"user_likes":0,"can_like":1,"can_publish":1},"reposts":{"count":0,"user_reposted":0},"views":{"count":2}}],"profiles":[{"id":213468131,"first_name":"Тимур","last_name":"Ивченко","sex":2,"screen_name":"jengas","photo_50":"https://pp.userap...328/iymw6YlJsRQ.jpg","photo_100":"https://pp.userap...327/J9eLMYmV_LI.jpg","online":1}],"groups":[{"id":156603484,"name":"Equestria in the Space","screen_name":"equestriaspace","is_closed":0,"type":"group","is_admin":1,"admin_level":3,"is_member":1,"photo_50":"https://pp.userap...089/u0_mBSE4E34.jpg","photo_100":"https://pp.userap...088/O6vENP0IW_w.jpg","photo_200":"https://pp.userap...086/rwntMz6YwWM.jpg"}]}}'
>>> l = json.loads(m)
>>> lst = []
>>> lst = l["response"]["items"]
>>> lst2 = lst[0]
>>> lst2['id']

Консоль:

1835

Ваш код не работает, потому что в нем не учитывается, что в "item" вложен список, и доступ к нему надо осуществлять как к элементу списка. lst — это список, а lst2 — словарь.

Добавить комментарий
Напишите свой ответ на данный вопрос.
Scroll Up