如何使用双引号解析json数据:
json.loads('
{
"time":"1410661614",
"text":"This is great",
"from":
{
"username":"mrb",
"id":"5071",
"full_name":"Free "Mrb"" #here is the problem
},
"id":"8090107"
}
')
python返回:
ValueError: Expecting ',' delimiter: line 1 column 107 (char 106)
答案 0 :(得分:1)
您可以通过转义双引号(\"
)
import json
json.loads("""
{
"time":"1410661614",
"text":"This is great",
"from":
{
"username":"mrb",
"id":"5071",
"full_name":"Free \\"Mrb\\""
},
"id":"8090107"
}
""")
如评论中所述,更好地修复json生成器以正确转义双引号,将难以解析和更正json文件。
答案 1 :(得分:1)
无论是谁编写了在字符串中发出未转义引号的程序,都需要认真的与...进行交谈。
正如Martijn所说,解析任意疯狂的引用并不容易。
OTOH,如果JSON格式正确,并且违规字符串不跨越线边界,那么它 不好。例如,
#! /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
<强>输出强>
"evil_name":"Free \"Mrb\"",
"good_name":"Alan Turing",