Python json parsing instance method

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.