我试图保存从URL中提取的Json数据,但是我很难将其保存到SQLite数据库中。在
Json:
{“地址”:“ff00000a70d57128”,“摄氏度”:26.8125,“华氏度”:80.2625}
我环顾四周,从中获得灵感:import threading
import json
import urllib.request
import sqlite3
def tempRequest():
# download raw json object
url = "http://10.0.0.111:8080/getdevice?device=type28_1"
data = urllib.request.urlopen(url).read().decode()
# parse json object
obj = json.loads(data)
print(obj)
tempRequest()
conn = sqlite3.connect('test.db')
c = conn.cursor()
def create_table():
c.execute("CREATE TABLE IF NOT EXISTS data(Celcius TEXT)")
def data_entry():
c.executemany('INSERT INTO data (Celcius) ''VALUES (:Celcius)
conn.commit()
c.close()
conn.close()
create_table()
data_entry()
当我运行这个程序时,我得到的是:
TypeError(f'JSON对象必须是str、bytes或bytearray,'
TypeError:JSON对象必须是str、bytes或bytearray,而不是省略号
我试图改变:
^{pr2}$
到obj = json.loads(str(data))
但我认为问题在于我如何处理数据库部分,任何建议都将不胜感激。
谢谢!在