简体   繁体   English

我可以从这个 json 中的键中删除双引号而不是值吗?

[英]Can I remove double quotes from keys in this json but not the values?

I have the following json string:我有以下 json 字符串:

'{"start_time": {"_gte": "2021-10-10 19:00:00"}, "end_time": {"_gte": "2021-10-10 19:30:00"}}'

and I need to get:我需要得到:

'{start_time: {_gte: "2021-10-10 19:00:00"}, end_time: {_gte: "2021-10-10 19:30:00"}}'

Is this possible?这可能吗?

I guess something like this should work:我想这样的事情应该有效:

import json
from typing import List, Tuple, Any


string = '{"start_time": {"_gte": "2021-10-10 19:00:00"}, "end_time": {"_gte": "2021-10-10 19:30:00"}}'


class MyDict(dict):
    def __repr__(self):
        return '{' + ', '.join([f'{k}: {v!r}' for k, v in self.items()]) + '}'


def hook_fn(value: List[Tuple[str, Any]]):
    return MyDict(value)


print(json.loads(string, object_pairs_hook=hook_fn))

it prints:它打印:

{start_time: {_gte: '2021-10-10 19:00:00'}, end_time: {_gte: '2021-10-10 19:30:00'}}

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

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