简体   繁体   English

如何使用 python 从字符串列表中删除生成的 json 之外的双引号

[英]how to remove double quotes out side of generated json from string list using python

My JSON is imput json { 'debug': None, 'traceback': None, 'rsname': None, 'resolutionStatus': 0, 'algorithmResponseInfo': None, 'rules': "['check']", 'rulesResults': '-RNCID Found', 'mmlCommand': 'tk_com', 'KMmmlCommand': 'cat "tk"', 'similarityScore': {'similarityUI': 100 }} which is invalid and I want to convert it into valid json I want to remove "" double quotes and single backslash from rules column (list) using python.我的 JSON 是输入 json { 'debug': None, 'traceback': None, 'rsname': None, 'resolutionStatus': 0, 'algorithmResponseInfo': None, 'rules': "['check'], ': '-RNCID Found', 'mmlCommand': 'tk_com', 'KMmmlCommand': 'cat "tk"', 'similarityScore': {'similarityUI': 100 }} 这是无效的,我想将其转换为有效json 我想使用 python 从规则列(列表)中删除""双引号和单反斜杠。 I have already tried the following but it didn't work -我已经尝试了以下方法,但没有奏效-

data = data.replace('"',"\\'").replace("'", '"').replace("True","true").\
             replace("None","null").replace("False","false").replace("\'", '"').replace("'", '"')

data = json.loads(data)

output is - data = { "debug": null, "traceback": null, "rsname": null, "resolutionStatus": 0, "algorithmResponseInfo": null, "rules": \ "["check"]", "rulesResults": "-RNCID Found", "mmlCommand": "tk_com", "KMmmlCommand": "cat "tk"", "similarityScore": { "similarityUI": 100 }} output is - data = { "debug": null, "traceback": null, "rsname": null, "resolutionStatus": 0, "algorithmResponseInfo": null, "rules": \ "["check"]", " rulesResults": "-RNCID Found", "mmlCommand": "tk_com", "KMmmlCommand": "cat "tk"", "similarityScore": { "similarityUI": 100 }}

but i need to remove extra backshlash and double quotes expected is - data = { "debug": null, "traceback": null, "rsname": null, "resolutionStatus": 0, "algorithmResponseInfo": null, "rules": ["check"], "rulesResults": "-RNCID Found", "mmlCommand": "tk_com", "KMmmlCommand": "cat "tk"", "similarityScore": { "similarityUI": 100 }} but i need to remove extra backshlash and double quotes expected is - data = { "debug": null, "traceback": null, "rsname": null, "resolutionStatus": 0, "algorithmResponseInfo": null, "rules": [“检查”],“rulesResults”:“找到-RNCID”,“mmlCommand”:“tk_com”,“KMmmlCommand”:“cat“tk””,“similarityScore”:{“similarityUI”:100 }}

What you have there is not JSON at all, but a Python literal.您所拥有的根本不是 JSON ,而是 Python 文字。 It's just that Python's literals are very similar to JSON.只是 Python 的字面量与 JSON 非常相似。 The safe way to convert this is using ast.literal_eval:转换它的安全方法是使用 ast.literal_eval:

from ast import literal_eval
import json
data = (
    "{ 'debug': None, 'traceback': None, 'rsname': None,"
    " 'resolutionStatus': 0, 'algorithmResponseInfo': None, "
    "'rules': \"['check']\", 'rulesResults': '-RNCID Found', "
    "'mmlCommand': 'tk_com', 'KMmmlCommand': 'cat \"tk\"',"
    " 'similarityScore': {'similarityUI': 100 }}"
python_doc = literal_eval(data)
json_str = json.dumps(python_doc)

# {"debug": null, "traceback": null, "rsname": null, "resolutionStatus": 0,
# "algorithmResponseInfo": null, "rules": "['check']",
#  "rulesResults": "-RNCID Found", "mmlCommand": "tk_com",
#  "KMmmlCommand": "cat \"tk\"", "similarityScore": {"similarityUI": 100}}
print(json_str)

The unsafe alternative to literal_eval is eval . literal_eval的不安全替代方案是eval The problem with eval is that if an attacker can control the input, they can cause arbitrary code to run. eval的问题在于,如果攻击者可以控制输入,他们就可以导致任意代码运行。

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

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