I have been studying flask programming in python recently, and now I am using jQuery’s easyui to write a web application. The problem now is that after querying the database, I want to return standard json data to the front end, but the program prompts:
File "D:\jQueryUI code\Flasktest.py", line 23, in p_statusnow
p_status_json = json.dumps({'total':total,'rows':[{'emp_sn':i.emp_sn,'name':i.name,'password':i.password,'emp_dept_sn':i.emp_dept_sn,'py_code':i.py_code,'wb_code':i.wb_code,'autograph':i.autograph} for i in user]},indent=4)
TypeError: <web.utils.IterBetter instance at 0x00000000033D8948> is not JSON serializable
code show as below:
@app.route('/p_statusnow',methods = ['GET','POST'])
def p_statusnow():
a=request.values.get('page')
print a
b=request.values.get('rows')
c=int(b)*(int(a)-1)
#user=db.query('select * from dic_user limit ')
user=db.select ('dic_user',offset=c,limit=b)
total=db.query('select count(*) from dic_user')
p_status_json = json.dumps({'total':total,'rows':[{'emp_sn':i.emp_sn,'name':i.name,'password':i.password,'emp_dept_sn':i.emp_dept_sn,'py_code':i.py_code,'wb_code':i.wb_code,'autograph':i.autograph} for i in user]},indent=4)
#print p_status_json
return p_status_json
The problem was found, the reason is not in json.dumps, but in
In this sentence, through print total, the output is found
That is to say, it is not feasible to get the total number of records in this way. I changed the program and assigned total to a:
Working normally.
object
不能直接放到json
格式里。会报错的。django
里,有提供serialize
把对象序列化的方法。至于
flask
I don’t know..