修复python烧瓶应用程序的JSON.PARSE语法错误

发布于 2025-02-11 23:11:31 字数 918 浏览 0 评论 0原文

我正在尝试使用JSON.PARSE在烧瓶应用程序中使用一些JSON数据。但是,我会遇到以下错误。

uckulting语法:在位置3的JSON中意外的令牌m

我怀疑这可能与报价有关,但不确定。代码在下面。

python(app.y)

import pandas as pd
from flask import Flask, render_template
import json

test_data = [['mack', 10, 988], ['john', 15, 200], ['jane', 14, 590]]
test_df = pd.DataFrame(test_data, columns=['Name', 'Age', 'Score'])
json_df = test_df.set_index('Name').to_json(orient='index')
    
@app.route("/hello")
def index():
    return render_template("index.html", json_df=json_df)

javaScript (在我的index.html文件中)

<script>
    const json_df = JSON.parse('{{ json_df|tojson }}');
    console.log(json_df);
</script>

,我希望能够与JavaScript中的数据一起工作,以:

const json_df = {mack:{Age:10,Score:988},john:{Age:15,Score:200},jane:{Age:14,Score:590}}

I'm trying to work with some JSON data into my JavaScript using JSON.parse in my Flask app. However, I'm getting the following error.

Uncaught SyntaxError: Unexpected token m in JSON at position 3

I'm suspecting it may be something to do with the quotes, but am not sure. Code is below.

Python (app.y)

import pandas as pd
from flask import Flask, render_template
import json

test_data = [['mack', 10, 988], ['john', 15, 200], ['jane', 14, 590]]
test_df = pd.DataFrame(test_data, columns=['Name', 'Age', 'Score'])
json_df = test_df.set_index('Name').to_json(orient='index')
    
@app.route("/hello")
def index():
    return render_template("index.html", json_df=json_df)

JavaScript (within my index.html file)

<script>
    const json_df = JSON.parse('{{ json_df|tojson }}');
    console.log(json_df);
</script>

Basically, I want to be able to work with the data in JavaScript, such that:

const json_df = {mack:{Age:10,Score:988},john:{Age:15,Score:200},jane:{Age:14,Score:590}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文