将包含双引号的字符串转换为json

时间:2019-08-30 09:44:13

标签: python json

Python Escape双引号字符并将字符串转换为json

我尝试用转义符转义双引号,但这也没有用

raw_string = '[{"Attribute":"color","Keywords":"green","AttributeComments":null},{"Attribute":" season","Keywords":["Holly Berry"],"AttributeComments":null},{"Attribute":" size","Keywords":"20"x30"","AttributeComments":null},{"Attribute":" unit","Keywords":"1","AttributeComments":null}]'

new_data = json.loads(raw_string)

它加载错误提示期望','分隔符:第1行第180列(字符179)

预期输出为JSON字符串

3 个答案:

答案 0 :(得分:2)

带有转义引号的正确JSON字符串应如下所示:

[{
    "Attribute": "color",
    "Keywords": "green",
    "AttributeComments": null
}, {
    "Attribute": " season",
    "Keywords": ["Holly Berry"],
    "AttributeComments": null
}, {
    "Attribute": " size",
    "Keywords": "20\"x30",
    "AttributeComments": null
}, {
    "Attribute": " unit",
    "Keywords": "1",
    "AttributeComments": null
}]

编辑: 您可以使用正则表达式来更正Python中导致有效json的字符串:

import re
import json

raw_string = '[{"Attribute":"color","Keywords":"green","AttributeComments":null},{"Attribute":" season","Keywords":["Holly Berry"],"AttributeComments":null},{"Attribute":" size","Keywords":"20"x30"","AttributeComments":null},{"Attribute":" unit","Keywords":"1","AttributeComments":null}]'

pattern = r'"Keywords":"([\d].)"x([\d].)""'
correctedString = re.sub(pattern, '"Keywords": "\g<1>x\g<2>"', raw_string)
print(json.loads(correctedString))

输出:

[{u'Keywords': u'green', u'Attribute': u'color', u'AttributeComments': None}, {u'Keywords': [u'Holly Berry'], u'Attribute': u' season', u'AttributeComments': None}, {u'Keywords': u'20x30', u'Attribute': u' size', u'AttributeComments': None}, {u'Keywords': u'1', u'Attribute': u' unit', u'AttributeComments': None}]

答案 1 :(得分:1)

raw_string = '[{"Attribute":"color","Keywords":"green","AttributeComments":null},{"Attribute":" season","Keywords":["Holly Berry"],"AttributeComments":null},{"Attribute":" size","Keywords":"20x30","AttributeComments":null},{"Attribute":" unit","Keywords":"1","AttributeComments":null}]'

new_data = json.loads(raw_string)

答案 2 :(得分:1)

首先将键值对从"Keywords":"20"x30""更改为"Keywords":"20x30"。 格式在您的代码中无效。如果此JSON不是由您创建或由其他来源生成的,请检查来源。您可以使用JSONLint检查JSON是否有效。只需将JSON粘贴到此处进行检查即可。

关于您的代码:

import json

raw_string = '[{"Attribute":"color","Keywords":"green","AttributeComments":null},{"Attribute":" season","Keywords":["Holly Berry"],"AttributeComments":null},{"Attribute":" size","Keywords":"20x30","AttributeComments":null},{"Attribute":" unit","Keywords":"1","AttributeComments":null}]'    
new_data = json.loads(raw_string)

由于new_data是一个列表。如果检查其第一个也是唯一的元素的类型,则使用print(type(new_data[0]))会发现它是您想要的dict

编辑:因为您说过要从数据库中获取此JSON,所以请检查是否所有JSON都带有此类格式错误。如果是,则要检查在哪里生成这些JSON。您的选择是在源头进行更正并手动更正,或者添加转义字符(如果这是一次性问题)。我强烈建议使用前者。