【问题标题】:JSON output to web from flask backend从烧瓶后端到 Web 的 JSON 输出
【发布时间】:2020-12-18 15:31:09
【问题描述】:

我已经从其他网络抓取了json数据,想输出到网络上,但是在flask的后端,它只返回一个列表,就像:[{'aa':'bb','cc':' dd'}]。如何将其转为 json 格式,以便 html 上的 js 脚本能够从后端获取这些 json 数据?

app.py:

url1 = requests.get('https://xxxxxxx/getJSON.aspx?jsontype=xxxx.aspx')
json_data = json.loads(url1.content.decode())
json_data = json.dumps(json_data)
json_data = json.loads(json_data)
return render_template('index.html', json_data=json_data)

html 脚本:

<script>
const json_org = JSON.parse({{ json_data }});
    const json_data = json_org
    ;

    const output = document.getElementById('output');

    output.innerHTML = json_data;
</script>

错误:

Uncaught SyntaxError: Unexpected token '&'

const json_org = JSON.parse([{&#56;name&#56;: &#56;Current&#56;},...}])

【问题讨论】:

标签: python json flask


【解决方案1】:

添加安全过滤器,这会有所帮助,

JSON.parse('{{ json_data |tojson | safe}}');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多