python parsing json data with double quotes

[亡魂溺海] 提交于 2019-12-24 11:27:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!