This article mainly introduces the small example of python parsing json data. the code is simple and practical. for details, refer to the usage of the latest monitoring of weather service latency, it is to check whether the weather data changes once every hour, and trigger an alarm if the weather data does not change three times. Since the data on the page is in json format, we should first consider how to parse the data on the page to obtain the desired field.
Generally, when we take data from a webpage, it is a string, for example:
The code is as follows:
Url_data = urllib2.urlopen (url). readline ()
When we get the page data in this way, url_data is a json string displayed on all pages, how can we convert this string to the dictionary format: time = json. loads (url_data) ["weatherinfo"] ["time"]
The loads () function of the json module can encode the original string as a dictionary, so it is much easier to find the key value of a field.
Some code is as follows:
The code is as follows:
Def getTime (url ):
Url_data = urllib2.urlopen (url). readline ()
Print url_data
Time = json. loads (url_data) ["weatherinfo"] ["time"]
Return time