JSON confusion
Jon Ribbens
jon+usenet at unequivocal.eu
Wed Aug 17 12:49:27 EDT 2016
On 2016-08-17, Steve Simmons <square.steve at gmail.com> wrote:
> I'm trying to write a small utility to find the closest railway station
> to a given (UK) postcode but the result is in JSON and I'm not familiar
> with it. I've got as far as extracting the JSON object and I can print
> the first level elements ("success" and "result") but I've totally
> confused myself about how to delve into the rest of the data structure.
> Can anyone point me to a 'how-to' for tackling a fairly complex SJON
> object or give me some pointers. ... or maybe point out if I'm taking an
> unnecessarily complex approach. Initially, I'm Looking to extract
> 'stationname', 'distance' and one or two of the coordinate pairs. To be
> honest, I'd rather have some hints rather than the whole solution
> otherwise I'll not learn anything :-) SteveS
It's not clear what the problem is. Does this help:
print(p_json["result"][0]["stationname"])
print(p_json["result"][0]["latlong"]["coordinates"])
?
(To extract an item from a JSON object you index it with a string,
e.g. ["foo"]; to extract an item from an array you use an integer,
e.g. [0].)
More information about the Python-list
mailing list