消除JSON文档中键值的前导和尾随反冲 [英] removing leading and trailing backlash in key value in JSON documents
本文介绍了消除JSON文档中键值的前导和尾随反冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下JSON结构.
I have the following JSON structure.
"bent": "{
\"ActiveT\": 6,
\"ErrorM\": \"None\",
\"Except\": \"None\",
\"HadErr\": \"false\",
\"HM\": 62,
\"NHM\": 57,
\"Parameter\": \"14331232706\",
\"ReturnCode\": \"3050\",
\"Severity\": \"info\",
\"Timestamp\": \"Tue July0209: 58: 16NZST2015\",
\"TId\": \"9891319709\",
\"UserInfo\": \"Unknown\",
}"
我想在JSON结构中的每个key
和value
的开头和结尾处删除\
,我想使用python正则表达式来做到这一点.
I want to remove the \
at the beginning and the end of each of the key
and value
in the JSON structure.I want to do this using python regular expressions.
推荐答案
使用 re.sub
>>> print re.sub(r'\\(?=")', '', string)
"bent": "{
"ActiveT": 6,
"ErrorM": "None",
"Except": "None",
"HadErr": "false",
"HM": 62,
"NHM": 57,
"Parameter": "14331232706",
"ReturnCode": "3050",
"Severity": "info",
"Timestamp": "Tue July0209: 58: 16NZST2015",
"TId": "9891319709",
"UserInfo": "Unknown",
}
正则表达式说明
-
\\
匹配\
(?=")
积极向前看.检查\
之后是否是"
(?=")
Positive look ahead. Checks if the \
is followed by "
将其替换为空字符串.
OR
>>> print string.replace('\\"', '"')
"bent": "{
"ActiveT": 6,
"ErrorM": "None",
"Except": "None",
"HadErr": "false",
"HM": 62,
"NHM": 57,
"Parameter": "14331232706",
"ReturnCode": "3050",
"Severity": "info",
"Timestamp": "Tue July0209: 58: 16NZST2015",
"TId": "9891319709",
"UserInfo": "Unknown",
}
这篇关于消除JSON文档中键值的前导和尾随反冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文