Jul-08-2018, 11:20 AM
Right, I am writing here to know, if anyone knows a better and more convenient way of using the data, which you get from the response of some API, which gives you a complex Dictionary structure.
Let say I have a class CallAPI, with method get_results(). This will give me the JSON.load results and store it in variable data.
Now let say I call it like this:
Stats:{ displayeValue #The stats and other keys below "Player" are the ones I want to reach and from those keys, I want to access the dict displayValue.
}
Kills:{ displayValue
}
}
]
The problem I have is if I want to get one specific thing I can do:
Just to show by what I mean by repeating, something from my code:
Let say I have a class CallAPI, with method get_results(). This will give me the JSON.load results and store it in variable data.
Now let say I call it like this:
api_get_results = CallAPI() data = api_get_results.get_results(). #Now the data has the complex Dictonarie inside of it for tessting lets imagine this dict[Player:{
Stats:{ displayeValue #The stats and other keys below "Player" are the ones I want to reach and from those keys, I want to access the dict displayValue.
}
Kills:{ displayValue
}
}
]
The problem I have is if I want to get one specific thing I can do:
get_stats = data['player']['stats']['displayValue']However, If I have 20 things which I need to access there will be a lot of same code repeating and I wanted to know if anyone knows a better way of pulling out those dict and storing them in a convenient way. Maybe a for cycle? And store those dict into a List?
Just to show by what I mean by repeating, something from my code:
score = data['stats']['p2']['score']['displayValue']
top1 = data['stats']['p2']['top1']['displayValue']
top3 = data['stats']['p2']['top3']['displayValue']
top5 = data['stats']['p2']['top5']['displayValue']
top6 = data['stats']['p2']['top6']['displayValue']
top10 = data['stats']['p2']['top10']['displayValue']
top12 = data['stats']['p2']['top12']['displayValue']
top25 = data['stats']['p2']['top25']['displayValue']
kdRatio = data['stats']['p2']['kd']['displayValue']
