A bit lost after much research. My code below parses the JSON to a dictionary I have thought using json load
response = json.load(MSW) # -->Will take a JSON String & Turn it into a python dict
Using the iteration below I return a series like this which is fine
{u'swell': {u'components': {u'primary': {u'direction': 222.5}}}}
{u'swell': {u'components': {u'primary': {u'direction': 221.94}}}}
ourResult = response
for rs in ourResult:
print rs
But how oh how do I access the 222.5 value. The above appears to just be one long string eg response[1] and not a dictionary structure at all.
In short all I need is the numerical value (which I assume is a part of that sting) so I can test conditions in the rest of my code. Is is a dictionary? With thanks as new and lost
-
1This question does not appear to be about parsing JSON, but about navigating nested Python dictionaries.Daniel Roseman– Daniel Roseman2014年06月30日 12:30:45 +00:00Commented Jun 30, 2014 at 12:30
2 Answers 2
You have to use python syntax as follows:
>>> print response['swell']['components']['primary']['direction']
222.5
2 Comments
Just access the nested dictionaries, unwrapping each layer with an additional key:
for rs in ourResult:
print rs['components']['primary']['direction']