I have a php file on server that prints some data from the mysql database in json format.
When I run my php file in my browser I can see the json array. Now I want to get that json array in a python file. Is there a way to do it?
here is the code i am using
import subprocess
import json
import codecs
proc = subprocess.Popen("http://www.rpi-connect.esy.es/getappliances?room=1", shell=True, stdout=subprocess.PIPE)
script_response = str(proc.stdout.read().decode('utf8'))
print(script_response)
I finally got the answer after trying a lot of different python libraries
import json
import urllib.request
url = "http://example.com/your-filename.php"
x = urllib.request.urlopen(url)
raw_data = x.read()
encoding = x.info().get_content_charset('utf8') # JSON default
print(raw_data) #this is data in string format
data = json.loads(raw_data.decode(encoding))
print(data) #this would be your json data
this works for python 3.4. for python 2.7 try using json.load() instead of json.loads()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With