o*****l 发帖数: 539 | 1 a JSON string like:
{
"a":"{\"type\":\"int\", \"value\":2}"
}
want to convert it to:
{
"a":"{\\"type\\":\\"int\\", \\"value\\":2}"
}
OR
{
"a":"{type:int, value:2}"
}
how to do it, thanks! | m*****e 发帖数: 47 | 2 >>> js = {
... "a":"{"type":"int", "value":2}"
... }
>>>
>>> js['a']
'{"type":"int", "value":2}'
>>>
>>> str(js['a']).replace('"', '')
'{type:int, value:2}'
>>>
>>> js['a'] = str(js['a']).replace('"', '')
>>>
>>> js
{'a': '{type:int, value:2}'}
>>> | m*****e 发帖数: 47 | 3 这个invalid:
{
"a":"{\"type\":\"int\", \"value\":2}"
}
See https://jsonchecker.com | m*****e 发帖数: 47 | | g*****g 发帖数: 390 | 5 # original string
s = '"a":"{"type":"int","value":2}'
print(s)
>>> "a":"{"type":"int","value":2}
s # notice the difference between s and print(s)
>>> '"a":"{"type":"int","value":2}'
# first solution looking for, don't like it, but it is what it is
s1 = repr(s).replace("'",'')
print(s1)
>>> "a":"{"type":"int","value":2}
s2 = s.replace('"','') # 2nd solution look for
print(s2)
>>> "a":"{type:int,value:2}
this is in jupyter notebook, >>> means output
【在 o*****l 的大作中提到】 : a JSON string like: : { : "a":"{\"type\":\"int\", \"value\":2}" : } : want to convert it to: : { : "a":"{\\"type\\":\\"int\\", \\"value\\":2}" : } : OR : {
|
|