简体   繁体   English

如何从 HTML 表单获取 json 并保持输入类型

[英]How to get json from HTML form and keep type of input

I can get json from HTML form but the problem is that all of value in side json are convert to string type.我可以从 HTML 表单中获得 json 但问题是 json 中的所有值都转换为字符串类型。 I have try both我都试过了
result = json.dumps(request.form) result = jsonify(request.form) but result still be the same.结果 = json.dumps(request.form) 结果 = jsonify(request.form) 但结果仍然相同。 HTML form Output Json HTML 形式Output Json

Is there any way to get the result like this { "department": "sales", "donation": 1538, "firstName": "abc", "lastName": "ccc" }有没有办法得到这样的结果 { "department": "sales", "donation": 1538, "firstName": "abc", "lastName": "ccc" }

HTML Code HTML 代码

<!doctype html>
<html>
   <body>
<div class="container">
        <form action="/display" method="post" id="employForm"
        <fieldset>
        <label>First Name
          <input type="text" name="firstName" placeholder="Joe" required>
        </label>
        <label>Last Name
          <input type="text" name="lastName" id="lastName" placeholder="Schmoe" required>
        </label>
        <label>Homeless cat donation
          <input type="number" name="donation" id="donation" placeholder=1234 required>
        </label>

        <label>
        Department
          <select name="department" required>
            <option value="sales">Sales</option>
            <option value="marketing">Marketing</option>
            <option value="developer">Developer</option>
            <option value="business">Business Relations</option>
            <option value="sysAdmin">Systems Administration</option>
            <option value="operation">Operation</option>
          </select>
        </label>
            Thanks
        </fieldset>
      <button class="button-primary" type="submit" value="Submit" form="employForm">SUBMIT!</button>
    </form>
    </body>
  </div>
 </html>

This is the python flask code这是 python flask 代码

from flask import Flask, render_template, request, jsonify
import json

app = Flask(__name__)
@app.route('/')
def hello():
     return render_template('layout.html')

@app.route('/display', methods=["GET", "POST"])
def display():   
    result = jsonify(request.form)
    return result

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

Taking notes from this Answer Here这里的答案中记笔记

JSON is untyped. JSON 无类型。 Anything sent in JSON format will be converted to a string;以 JSON 格式发送的任何内容都将转换为字符串; so its not possible to send the donation value as an integer.因此无法将donation值作为 integer 发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 HTML 输入表单中保持文本格式? - How to keep text formatting in HTML Input Form? 在python中保存来自html表单的输入值时,为什么仍然收到非类型错误? - Why do I keep receiving a non type error when saving input value from html form in python? 如何在不从 HTML 重新加载到 flask 的情况下获取输入表单? - How to get input form without reloading from HTML to flask? 如何从通过 HTML 表单传递的 JSON 对象“获取”请求 - How to “GET” request from JSON objects passed through HTML form 如何使用django在html中运行python脚本并从html输入表单获取变量 - How to run python script in html using django and get variable from html input form 如何使用flask将测试输入从html表单获取到另一个python脚本? - How to get test input from an html form to another python script using flask? 如何从输入表单获取数据 - How to get data from input form 如何从输入中获取 html 并在 Python Django 中保存为变量 - How to get html form input and save as variable in Python Django 如何在Django中从表单或上传的输入文件获取输入? - How to get input either from a form or as uploaded input file in Django? 如何将 html 模板中表单的输入附加到 url? - How do I append an input from a form in a html template to a url?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM