-3

I'm playing around with the league of legends api, anyway I made this very simple program.

import requests
region = input("Enter a region ")
summonerName = input("Enter Summoner Name ")
apikey = input("Enter APIKey ")
r = requests.get(url="https://" + region + ".api.pvp.net/api/lol/" + region + "/v1.4/summoner/by-name/" + summonerName + "?api_key=" + apikey)
print(r.json())

and this is what it returns.

{'hiimeric': {'revisionDate': 1478543641000, 'name': 'Hi Im Eric', 'id': 36843151, 'profileIconId': 13, 'summonerLevel': 30}}

so my question is now, how could I for example get it to only post 'name' or only 'name' and 'profileIconId'? Thanks!

Eric
  • 7
  • 2
  • Are you assuming that the value `'hiimeric' is known or unknown to you? (The answers so far assume that it is known.) Are you sure that values for only one user will be returned? – Rory Daulton Nov 09 '16 at 19:32
  • It's just a dictionary, containing another dictionary. Perhaps you wanted to [read the tutorial on dictionary use](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)? – Martijn Pieters Nov 09 '16 at 19:32
  • 1
    Possible duplicate of [Accessing elements of python dictionary](http://stackoverflow.com/questions/5404665/accessing-elements-of-python-dictionary) – Moinuddin Quadri Nov 09 '16 at 20:23

2 Answers2

0

A JSON object is actually just a combination of dictionaries and lists. Hence, you can print the name and profileIconId with the following:

print(r.json()['hiimeric']['name'])
print(r.json()['hiimeric']['profileIconId'])
0

Json objects are two kinds: dict and list. In this case, it is dictionary. If you want to know explicit, use type function like this.

obj = r.json()
print(type(obj))
dohvis
  • 164
  • 2
  • 7