javascript无法从python转换为对象json,只能将其视为字符串

mkshixfv  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(156)

我在python中创建了一个动态json结构
在将其发送到客户端之前,我将其转换为字符串:

json.dumps(db_list)

然后在客户端,它呈现如下:(是从chrome中的视图源复制的)

<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有什么问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题