【问题标题】:Converting string containing double quotes to json将包含双引号的字符串转换为json
【发布时间】:2020-01-03 13:34:36
【问题描述】:

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字符串

【问题讨论】:

  • 这里的值不正确 ---> :"20"x30"",你需要解决这个问题
  • 这里有格式错误:"Keywords":"20"x30"",比如改成"Keywords":"20x30"
  • @RomanPerekhrest 感谢您的回复,但这是我从数据库中获取的数据
  • 你的python字符串raw_string是一个有效的字符串,但不是有效的json。您需要先修复字符串。你是怎么拿到那根弦的?为什么你认为它可能是json

标签: python json


【解决方案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 是在哪里生成的。如果这是一次性问题,您的选择是在源头更正并手动更正或添加转义字符。我强烈推荐前者。

【讨论】:

  • 很好的答案,但你怎么知道"Keywords":"20x30" 是正确的?如果是"Keywords":"20'x30'" 那不也是正确的吗?
  • @akshayks 我知道它无效的 json 我想正确格式化字符串以便它可以转换为 json
  • @Shubham:没有办法找到并纠正它。在你看到它之前应该已经更正了。
  • @quamrana 是的,我认为是这样。您可能是对的,这就是为什么我要求 OP 检查源代码是否确实是他想要的格式。
【解决方案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}]

【讨论】:

  • 感谢您的回复 有什么办法可以用转义字符将字符串转换成这种格式
  • 感谢您的回复,您的解决方案解决了我的问题。
【解决方案3】:
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)

【讨论】:

  • 感谢您的回复我想将字符串转换为python字典
  • 我已经编辑了我的代码。现在试试看。关键字值存在错误,需要更正。
  • 感谢您的回复,我知道问题在"Keywords":"20"x30"" 之内,但我不知道如何解决它
  • 你能解释一下你的意思吗?
  • 我的意思是,你从哪个数据库中获取这个?以及如何填充数据库?错误出现在您填充数据库的过程中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2021-12-27
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多