Python parsing JSON instance method

Source: Internet
Author: User
Recently in the weather operation of the delay monitoring, is every one hours to check whether the weather data changes, three times do not change the alarm. Because the page gives the data in JSON format, so how to parse the data on the page, so that we want to get the field is the first thing we consider.
In general, when we take the data from a webpage, it is a string, such as:

Copy the Code code as follows:


Url_data = Urllib2.urlopen (URL). ReadLine ()



When we get the page data this way, Url_data is displaying a JSON string on all pages, so how do we turn this string into a dictionary format: time = Json.loads (Url_data) ["Weatherinfo"] ["Time"]

The function loads () of the JSON module encodes the original string into a dictionary, so that we can find the key value of a field much more convenient.
Some of the code is as follows:

Copy the Code code 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.