How to get full json data using jsom.dumps?
天蓬老师
天蓬老师 2017-05-18 10:56:12
0
3
672

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
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
仅有的幸福

The problem was found, the reason is not in json.dumps, but in

total=db.query('select count(*) from dic_user')

In this sentence, through print total, the output is found

<web.utils.IterBetter instance at 0x00000000037D0888>

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:

    total=db.query('select count(*) as num from dic_user')
    for i in total:
        a=i.num
    p_status_json = json.dumps({'total':a,'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

Working normally.

给我你的怀抱
from flask import jsonify
....


def foo():
    return jsonify({'test':1})
给我你的怀抱

object 不能直接放到 json 格式里。会报错的。
django 里,有提供 serialize 把对象序列化的方法。
至于 flask I don’t know..

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!