从flask中的mysql解析json以从记录中获取字段

时间:2016-06-04 21:15:01

标签: python mysql json

请注意,我对于瓶子和蟒蛇是新手,我感谢任何人给予的任何帮助。我想要访问我的JSON响应的一个字段(只是字段而不是整个响应),我应该如何解析响应。下面附上回复的图片,谢谢。

这是我的主线

from flask import Flask,render_template,request
from Qhandler import Qhandler
from MakePlayer import MakePlayer
app = Flask(__name__)

@app.route('/createplayer',methods=['GET','POST'] )
def showCreatePlayer():
    if request.method == 'POST':
        MakePlayer(request.form['playername'],request.form['playerteam'],request.form['playerrole'], request.form['playerpos'])
        return "created player: <br>"+request.form['playername']+" "+request.form['playerteam']+" "+request.form['playerrole']+" "+request.form['playerpos']

    return render_template("createPlayer.html")

@app.route('/sucess')
def success():
    return "success"

@app.route('/showplayers')
def showPlayers():
    Q = Qhandler()
    return Q.displayQuery(""" select * from Player""")

if __name__ == '__main__':
    app.run(debug=True)


这是我的查询处理程序

from flask import Flask, jsonify, json
from flaskext.mysql import MySQL

class Qhandler(object):
    #global mysql
    global cursor
    global connection
    global mysql
    # database connection
    app = Flask(__name__)

    mysql = MySQL()
    app.config['MYSQL_DATABASE_USER'] = 'root'
    app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
    app.config['MYSQL_DATABASE_DB'] = 'Optimizer'
    app.config['MYSQL_DATABASE_HOST'] = 'localhost'
    mysql.init_app(app)



    def ins(self,query):
        try:
            connection=mysql.connect()
            cursor = connection.cursor()
            cursor.execute(query)
            connection.commit()

        except:
            print "error running query"

        finally:
            #cursor.close()
            connection.close()

    def displayQuery(self,query):
        try:
            connection = mysql.connect()
            cursor = connection.cursor()
            cursor.execute(query)
            fetchedData = cursor.fetchall()
            fetchedData = jsonify(fetchedData)
            #fetchedData = json.dumps(fetchedData)
            #record = json.loads(fetchedData)
            #print "the resonse is here:"
            return fetchedData

        except:
            print "error running query"

        finally:
            #cursor.close()
            connection.close()

目前的回应是

screenshot of results

1 个答案:

答案 0 :(得分:0)

使用&#34; fetchedData = json.dumps(fetchedData)&#34;而不是&#34; fetchedData = jsonify(fetchedData)&#34;然后创建一个json解码器并解析响应,参考下面:

def displayQuery(self,query):
        try:
            connection = mysql.connect()
            cursor = connection.cursor()
            cursor.execute(query)
            fetchedData = cursor.fetchall()
            fetchedData = json.dumps(fetchedData)
            #create a json decoder
            d = json.JSONDecoder()
            fieldPlayerName = d.decode(fetchedData)
            #parse the json that is returned ( fieldPlayerName[0][1])
            print "should print the field with the player name",fieldPlayerName[0][1]
            return fieldPlayerName[0][1]