weixin_33724659 2017-10-10 16:11 采纳率: 0%
浏览 37

获取json的值

Aloha, i am trying to follow this tutorial and here is a Git of it

I was pretty successfull with it but now i am trying to modify this code a little bit :)

from flask import Flask, jsonify, request, render_template

app = Flask(__name__)

Persons = [{'name' : 'Arnold' , 'email' : 'disceps@gmail.com'},
           {'name' : 'Alex Mikro' , 'email' : 'kovalsky@gmail.com'}, 
           {'name' : 'Phost Arnold' , 'email' : 'arn@gmx.de'}]

So, i've created the following value

For now, i would like to get a POST responce from this...So, if a name is equall to the typed name, there should be a responce..

@app.route('/pers', methods=['POST'])
def addOne():
    email = request.form['email']
    name = request.form['name']

if name and email:
    pers = [persons for persons in Persons if persons['name'] == name]
    pers[0]['name'] = request.get_json(force=True).get('name')    
    if name == pers[0]:
        return jsonify({'name' : name})


return jsonify({'error' : 'Missing data!'})

but unfortunately,this doesn't work..

p.s. the code mentioned in the video

def addOne():
email = request.form['email']
name = request.form['name']

if name and email:
    newName = name[::-1]
    return jsonify({'name' : newName})

return jsonify({'error' : 'Missing data!'})

is working without any troubles :) So i think, there is a problem in my IF-Statement...

  • 写回答

1条回答 默认 最新

  • weixin_33676492 2017-10-11 16:09
    关注

    OK. A lot going on in there. Per Davidism, please explain what you are attempting to do.

    Issues:

    • You have a list with multiple dictionaries in your original example.
    • Then you have a form where you are taking in values from two fields('email' and 'name') and assigning them to variable (email and name)
    • Then, if the variables are populated, you are constructing a list (pers) with a list comprehension, where pers will contain any dictionary from the Persons list with a 'name' value that matches the name variable. Why?
    • Then I lose you. The code after that seems to do multiple unnecessary tasks.

    Almost everything under if name and email: makes no sense as a whole. If you explain what your intent is, I think someone can help. As it is, I can't see what you planned to do.

    The example you provide from the video is not doing what you are doing. It is making a new variable newName from the variable name which is derived from the form (name = request.form['name']). Why it is making newName a reversed version of name I don't know, but even with that it is doing something far simpler than you appear to be attempting.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧