问题
How do you parse json data with double quotes within:
json.loads('
{
"time":"1410661614",
"text":"This is great",
"from":
{
"username":"mrb",
"id":"5071",
"full_name":"Free "Mrb"" #here is the problem
},
"id":"8090107"
}
')
python returns:
ValueError: Expecting ',' delimiter: line 1 column 107 (char 106)
回答1:
You can easily fix this issue by escaping the double quote (\"
)
import json
json.loads("""
{
"time":"1410661614",
"text":"This is great",
"from":
{
"username":"mrb",
"id":"5071",
"full_name":"Free \\"Mrb\\""
},
"id":"8090107"
}
""")
As said in the comments, better fix the json generator to properly escape the double quote, it will be hard to parse and correct the json file.
回答2:
Whoever wrote the program that emits those unescaped quotes inside strings needs a serious talking to...
As Martijn said, parsing arbitrary crazy quotes is not easy.
OTOH, if the JSON is otherwise well-formed, and the offending strings don't cross line boundaries, then it's not so bad. Eg,
#! /usr/bin/env python
''' Escape quotes in malformed JSON value strings
Written by PM 2Ring 2014.09.19
'''
import re
data = [
''' "evil_name":"Free "Mrb"",''',
''' "good_name":"Alan Turing",'''
]
for line in data:
pre, val = line.split(':')
parts = re.split('(")', val)
n = parts.count('"')
if n > 2:
i = 1
a = []
for c in parts:
if c == '"':
if 1 < i < n:
c = '\\"'
i += 1
a.append(c)
line = pre + ':' + ''.join(a)
print line
Output
"evil_name":"Free \"Mrb\"",
"good_name":"Alan Turing",
来源:https://stackoverflow.com/questions/25931016/python-parsing-json-data-with-double-quotes