我如何从Flask {j}中的json格式中提取数据

时间:2017-06-21 22:27:09

标签: json forms flask

我需要帮助我现在不知道如何从json格式中提取数据我想在json中进行搜索输入,在我从MYSQLdb获取信息之后我得到了这个

{"result":[[21,"bogdan333","bogdan333",0,"","templates/UPLOAD_FOLDERp7.jpg","",1],[22,"Bogdan3333","Bogdan3333",0,"","profile_pic/p12.jpg","back_pic/p5.jpg",1]]}

我现在不知道如何提取名称" Bogdan3333" 我的代码

<script type="text/javascript">
function getState3(value){
    $.post("/search_index?medic={{session['medic']}}&id={{id_general_user}}&connect=connect",{searching:value},function(data2){
        $(".solutii").html(data2);
    });
}
</script>
 <form id="custom-search-form" method="POST" class="form-search form-horizontal pull-right" action="">
                <div class="input-append span12">
                    <input type="text" class="search-query mac-style" onkeyup="getState3(this.value)"  ng-model="firstName" placeholder="Search">

                    <div class="finder-search"><p class="firstName"> {{ firstName }}</p></div>

                    <button type="submit" class="btn"><i class="icon-search"></i></button>
                </div>
            </form>

这是烧瓶部分:

@app.route('/search_index',methods=['POST','GET'])
def search_index():
    if request.method=="POST":
        search=request.form['searching']
        sql_search="SELECT * FROM register WHERE medic=1 AND uname LIKE '%"+str(search)+"%' LIMIT 20"
        conn=con.execute(sql_search)
        print(conn)
        get=con.fetchall()
        print(get)
##        return redirect(url_for("index",medic=session['medic'],id=session['id'],connect='connect'))
        return jsonify(result=get)

1 个答案:

答案 0 :(得分:0)

要从json中提取数据,您需要将字符串转换为适当的数据对象。使用Pythons json模块。

import json
data = json.loads(yourjsonstring)

# now access your data
res = data['result']