-
-
Save sirleech/2660189 to your computer and use it in GitHub Desktop.
| # Load Json into a Python object | |
| import urllib2 | |
| import json | |
| req = urllib2.Request("http://localhost:81/sensors/temperature.json") | |
| opener = urllib2.build_opener() | |
| f = opener.open(req) | |
| json = json.loads(f.read()) | |
| print json | |
| print json['unit'] | |
| # Array example | |
| import urllib2 | |
| import json | |
| req = urllib2.Request("http://vimeo.com/api/v2/video/38356.json") | |
| opener = urllib2.build_opener() | |
| f = opener.open(req) | |
| json = json.loads(f.read()) | |
| print json | |
| print json[0]['title'] |
Thanks, i use your first example. I think the content will be cached. Any solution for that?
It helps me so much,Thanks
+1 for you sir
So whats the advantage of using .build_opener instead of urlopen?
simple and understandable
I would suggest you don't create a variable named 'json' that overloads the class named 'json' in line 19.
import requests
import json
import urllib
url="http://api.open-notify.org/iss-pass.json"
r=requests.get(url)
t=json.loads(r.content)
for i in range(len(t)):
print(t[i]['state'])
I would suggest using requests
import requests
r = requests.get('URL-HERE')
print r.json()
This is a bit confusing https://gist.github.com/sirleech/2660189#file-gistfile1-py-L19, the variable name is shadowing the library json here. Something like json_response could work better here.
This is very helpful. Thank you.