JavaScript 无法从 Python Flask 转换为对象 JSON,它只能将其视为字符串

JavaScript can't convert to object JSON from Python Flask, it only sees it as a string

提问人:user63898 提问时间:7/8/2021 最后编辑:halferuser63898 更新时间:11/8/2023 访问量:121

问:

我正在 Flask 中创建一个动态 JSON 结构。

在将其发送到客户端之前,我将其转换为字符串:

json.dumps(db_list)

然后在客户端,它被呈现为:(从Chrome中的View Source复制):

<input type="hidden"
       id="data_from_server"
       name="data_from_server"
       value='[{&#39;data_source_name&#39;: &#39;FOO&#39;, &#39;db_servers_entry_list&#39;: [{&#39;yml_props&#39;: {&#39;version&#39;: 12, &#39;dir&#39;: &#39;dir/Oracle&#39;, &#39;default&#39;: True, &#39;schema&#39;: {&#39;driver&#39;: &#39;xxOracleDriver&#39;}}, &#39;display&#39;: &#39;oracle-12c-xxxx&#39;, &#39;ip&#39;: &#39;xsx&#39;, &#39;port&#39;: &#39;1521&#39;, &#39;selected&#39;: True}, {&#39;yml_props&#39;: {&#39;dir&#39;: &#39;sql-scripts/MySQL&#39;, &#39;version&#39;: 5.7}, &#39;display&#39;: &#39;mysql-5.7-xxxx-x&#39;, &#39;ip&#39;: &#39;dd.dd&#39;, &#39;port&#39;: &#39;3306&#39;, &#39;selected&#39;: False}, {&#39;yml_db&#39;: {&#39;dir&#39;: &#39;sql-scripts/MsSQL&#39;, &#39;version&#39;: 2017}, &#39;display&#39;: &#39;mssql-s&#39;, &#39;ip&#39;: &#39;10.x.x.x&#39;, &#39;port&#39;: &#39;1433&#39;, &#39;selected&#39;: False}], &#39;clean&#39;: {&#39;create&#39;: &#39;db/main.sql&#39;, &#39;polulate&#39;: [{&#39;xxxx&#39;: {&#39;enabled&#39;: True, &#39;scripts&#39;: [&#39;mssql/*-sql&#39;, &#39;clean/*.sql&#39;]}}, {&#39;xx&#39;: {&#39;enabled&#39;: True, &#39;scripts&#39;: [&#39;dir/*.sql*.sql&#39;]}}, {&#39;DI&#39;: {&#39;enabled&#39;: True, &#39;scripts&#39;: [&#39;dir/*.sql&#39;]}}]}, &#39;services:&#39;: [&#39;xxxxxxxx&#39;, &#39;xxxxxx&#39;, &#39;xxxxpm&#39;, &#39;xxxxuser&#39;]}, {&#39;data_source_name&#39;: &#39;dt&#39;, &#39;db_servers_entry_list&#39;: [{&#39;yml_db&#39;: {&#39;version&#39;: 12, &#39;default&#39;: True, &#39;schema&#39;: {&#39;driver&#39;: &#39;xxOracleDriver&#39;, &#39;user&#39;: &#39;xxx&#39;, &#39;password&#39;: &#39;yyyy&#39;, &#39;ip&#39;: &#39;10.x.x.x&#39;, &#39;port&#39;: 1541, &#39;sid&#39;: &#39;zzz&#39;, &#39;url&#39;: &#39;jdbc:oracle:thin:yyyy/yyyy@$10.x.x.x:1541/zzz&#39;}}, &#39;display&#39;: &#39;oracle-12c-xxxx&#39;, &#39;ip&#39;: &#39;xsx&#39;, &#39;port&#39;: &#39;1521&#39;, &#39;selected&#39;: True}, {&#39;yml_db&#39;: {&#39;version&#39;: 5.7}, &#39;display&#39;: &#39;mysql-5.7-xxxx-x&#39;, &#39;ip&#39;: &#39;dd.dd&#39;, &#39;port&#39;: &#39;3306&#39;, &#39;selected&#39;: False}, {&#39;yml_properties&#39;: {&#39;version&#39;: 2017}, &#39;display&#39;: &#39;mssql-s&#39;, &#39;ip&#39;: &#39;10.x.x.x&#39;, &#39;port&#39;: &#39;1433&#39;, &#39;selected&#39;: False}], &#39;clean&#39;: None, &#39;services:&#39;: [&#39;xxxxxxxx&#39;]}]'>

当我这样做时,在客户端:

typeof(JSON.parse(JSON.stringify(document.getElementById("data_from_server").value)))

我一直觉得它是“字符串”。

当我这样做时:

typeof(JSON.parse(document.getElementById("data_from_server").value))

我得到:

VM3503:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 2
    at JSON.parse (<anonymous>)
    at <anonymous>:1:13

为什么用 ?我的 JSON 有什么问题?"&#39;"

JavaScript Python JSON 解析 客户端

评论

0赞 Epsi95 7/8/2021
value不是JSON,你能展示一点样本吗?json.dumps(db_list)
0赞 user63898 7/9/2021
当它循环从 python dict 的 json 构建时显示它有问题 为什么值不是 JSON ?你可以看到它的 json ....

答: 暂无答案