Python parse JSON instance method _python

Source: Internet
Author: User
Tags instance method readline python parse json

Recently in the weather business delayed monitoring, is every one hours to check the weather data is changed, 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 get the field we want is the first consideration of the issue.
In general, when we take down data from a Web page, it's a string, like:

Copy Code code as follows:

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


When we get the page data like this, Url_data is the entire page displaying a JSON string, then how do we convert this string into a dictionary format: time = Json.loads (Url_data) ["Weatherinfo"] ["Time"]

The function loads () of the JSON module allows you to encode 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 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

Related Article

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.