Home  >  Q&A  >  body text

How to get full json data using jsom.dumps?

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

reply all(3)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-18 10:58:12

    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.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:58:12

    from flask import jsonify
    ....
    
    
    def foo():
        return jsonify({'test':1})

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:58:12

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

    reply
    0
  • Cancelreply