While I am trying to retrieve values from JSON string, it gives me an error:
data = json.loads('{"lat":444, "lon":555}') return data["lat"]
But, if I iterate over the data, it gives me the elements (lat
and lon
), but not the values:
data = json.loads('{"lat":444, "lon":555}') ret = '' for j in data: ret = ret + ' ' + j return ret
Which returns: lat lon
What do I need to do to get the values of lat
and lon
? (444
and 555
)
So first thing you need to import the 'json' module into the file. Then create a simple json object string in python and assign it to a variable. Now we will use the loads() function from 'json' module to load the json data from the variable. We store the json data as a string in python with quotes notation.
If you want to iterate over both keys and values of the dictionary, do this:
for key, value in data.items(): print key, value
What error is it giving you?
If you do exactly this:
data = json.loads('{"lat":444, "lon":555}')
Then:
data['lat']
SHOULD NOT give you any error at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With