简体   繁体   English

JSON 转储由单引号包围并包含转义单引号的字符串

[英]JSON Dump a string surrounded by single quotes and containing escaped single quotes

I'm working on a Django web app and I have a problem.我正在开发 Django web 应用程序,我遇到了问题。 I have a page where i list all the results in a MySQL table, and it works fine.我有一个页面,在其中列出了 MySQL 表中的所有结果,它工作正常。

{% for row in db_list %}
    <tr>
        <th scope="row">{{ row.description }}</th>
        <td>
            <button type="submit" name="instance" value="{{ row }}" class="btn btn-primary" title="OPEN TABLE" id="test"><i class="fas fa-list-ul"></i></button>
        </td>
    </tr>
{% endfor %}

Depending on the selection, I have to show the relations of the chosen table, and it works fine with a workaround, but it's not generalistic.根据选择,我必须显示所选表的关系,并且可以通过变通方法正常工作,但这并不通用。

What I'm doing is, as you see, transferring back a single row of the json.如您所见,我正在做的是传回 json 的单行。

The db_list is: db_list 是:

{
    "db_list": [
        {
            "description": "description",
            "table_name": "table_name",
            "database_name": "database_name"
        },
        {
            "description": "description",
            "table_name": "table_name",
            "database_name": "database_name"
        },
        {
            "description": "description",
            "table_name": "table_name",
            "database_name": "database_name"
        }
    ]
}

I would expect to transfer a json like this:我希望像这样传输 json:

{ 
    "description": "description", 
    "table_name": "table_name", 
    "database_name": "database_name" 
}

In this case, I would treat it with the json.dumps() and retrieving the fields I need to execute the query with a simple command and everything would be fine. 在这种情况下,我将使用 json.dumps() 来处理它,并使用一个简单的命令检索执行查询所需的字段,一切都会好起来的。

But I get an escaped string, surrounded by two quotes.. 但我得到一个转义字符串,被两个引号包围。
 '{ \'description\': \'description\', \'table_name\': \'table_name\', \'database_name\': \'database_name\' }'

This means it's a very weird string. 这意味着它是一个非常奇怪的字符串。

Because of quotes, if I try to `json.dump()` this string, I obtain a further surround... 由于引号,如果我尝试 `json.dump()` 这个字符串,我会获得进一步的环绕......
 '"{ \'description\': \'description\', \'table_name\': \'table_name\', \'database_name\': \'database_name\' }"'



If i try to do dumped_json['description'] it says the parameter must be integer.. Because of course it still is not a dict.如果我尝试做 dumped_json['description'] 它说参数必须是 integer .. 因为它当然仍然不是一个字典。 Even if i dumped it.即使我把它扔了。

I already tried surrounding with a dict(), but no.我已经尝试过使用 dict(),但没有。
I already tried replacing the quotes with other quotes or with nothing, but no.我已经尝试用其他引号或什么都不替换引号,但没有。 Can't dump without quotes, and I keep seeing escapes if I replace with double quotes.没有引号就不能转储,如果我用双引号替换,我会一直看到转义。

The problem is: it technically keeps being a string.问题是:从技术上讲,它一直是一个字符串。 If i try to print it, of course it has no \ escapes.如果我尝试打印它,当然它没有 \ 转义。 the [0] of the json is the { (instead, is the " in case i did the dump) and not the '. Already tried the ascii decode and all this stuff. It seems the \ escapes are not "physically" present in the string.. json 的 [0] 是 { (相反,是“如果我做了转储)而不是 '。已经尝试过 ascii 解码和所有这些东西。似乎 \ 转义不是“物理”存在于字符串..

I want to give credit to my boy @Seth250 who gave me half of the solution我想感谢我的男孩@Seth250,他给了我一半的解决方案

What I had to do is simply using the literal_eval but, instead of dumping it with the json lib, simply surrounding the literal_eval'ed string into a dict.我所要做的就是简单地使用literal_eval,而不是用json lib 转储它,而是简单地将literal_eval'ed 字符串包围成一个字典。

instance = ast.literal_eval(string_with_escapes)
instance_json = dict(instance)

the result will, infact, be:结果实际上是:

{
    'description': 'description', 
    'table_name': 'table_name', 
    'database_name': 'database_name'
}

which is exactly what I expected and now i can fully retrieve the fields i need. 这正是我所期望的,现在我可以完全检索我需要的字段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM