简体   繁体   中英

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. I have a page where i list all the results in a MySQL table, and it works fine.

{% 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.

The db_list is:

{
    "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:

{ 
    "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.

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...
 '"{ \'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. Even if i dumped it.

I already tried surrounding with a dict(), but no.
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..

I want to give credit to my boy @Seth250 who gave me half of the solution

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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