Python:Can';t将字符串转换为JSON
在过去的几个小时里,我一直在努力将字符串转换成JSON dict。我尝试了JSON.loads(…这会引发一个错误:Python:Can';t将字符串转换为JSON,python,json,decode,Python,Json,Decode,在过去的几个小时里,我一直在努力将字符串转换成JSON dict。我尝试了JSON.loads(…这会引发一个错误: requestInformation = json.loads(entry["request"]["postData"]["text"]) //throws this error json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: 使用混合的re.sub('\\',''
requestInformation = json.loads(entry["request"]["postData"]["text"])
//throws this error
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes:
使用混合的re.sub('\\','',mystring),mystring.sub(…来去除斜杠,但没有效果。我的问题字符串如下所示
'{items:[{n:\\'PackageChannel.GetUnitsInConfigurationForUnitType\\',ps:[{n:\\'unitType\\',v:"ActionTemplate"}]}]}'
这个字符串的来源是Google Chrome中的一个HAR转储。我认为这些反斜杠是在某个地方被转义的,因为HAR文件的大部分不包含它们,但它们通常出现在任何标记为“文本”的字段中
编辑我最终放弃了将上面的文本转换为JSON,而是选择了正则表达式。有时出现斜杠,有时它们不基于我在其中查看文本的内容,这使得使用起来很困难。您需要在JSON字符串之前加上一个r,或者用\替换所有字符\\ 这项工作:
import json
validasst_json = r'''{
"postData": {
"mimeType": "application/json",
"text": "{items:[{n:'PackageChannel.GetUnitsInConfigurationForUnitType',ps:[{n:'unitType',v:\"Analysis\"}]}]}"
}
}'''
txt = json.loads(validasst_json)
print(txt["postData"]['mimeType'])
print(txt["postData"]['text'])
json
模块需要一个字符串,其中键也用双引号括起来
因此,下面的字符串将起作用:
mystring = '{"items":[{"n":"PackageChannel.GetUnitsInConfigurationForUnitType", "ps":[{"n":"unitType","v":"ActionTemplate"}]}]}'
myjson = json.loads(mystring)
此函数应删除双反斜杠,并在关键点周围加双引号
import json, re
def make_jsonable(mystring):
# we'll use this regex to find any key that doesn't contain any of: {}[]'",
key_regex = "([\,\[\{](\s+)?[^\"\{\}\,\[\]]+(\s+)?:)"
mystring = re.sub("[\\\]", "", mystring) # remove any backslashes
mystring = re.sub("\'", "\"", mystring) # replace single quotes with doubles
match = re.search(key_regex, mystring)
while match:
start_index = match.start(0)
end_index = match.end(0)
print(mystring[start_index+1:end_index-1].strip())
mystring = '%s"%s"%s'%(mystring[:start_index+1], mystring[start_index+1:end_index-1].strip(), mystring[end_index-1:])
match = re.search(key_regex, mystring)
return mystring
我无法在您编写的第一个字符串上直接测试它,双引号/单引号不匹配,但在最后一个代码示例中它可以工作。这是有效的javascript对象,但无效的json和无效的python dict可能是松散的json解析器。错误不是关于反斜杠,而是抱怨键没有双引号regex're'模块,
>>json.load(re.sub(“(\w+):”,r''\1:”,a.replace(“,”){u'items':[{u'ps':[{u'v':u'Analysis',u'n':u'unitType',u'n':u'PackageChannel.GetUnitsInConfigurationForUnitType'}}他正在解码゙文本゙ 我只是在展示如何将字符串转换成JSON dict,他在这方面遇到了一些问题。然后这不是答案,投票不公平,问题标题说不能将字符串转换成JSON,这就是我试图做的。除此之外,我们在这里还看到了什么小故障?您正在解码不同的JSON对象,OP已经把它作为py了thon对象-条目[“请求”][“postData”][“文本”]-无错误-阅读更多代码。
import json, re
def make_jsonable(mystring):
# we'll use this regex to find any key that doesn't contain any of: {}[]'",
key_regex = "([\,\[\{](\s+)?[^\"\{\}\,\[\]]+(\s+)?:)"
mystring = re.sub("[\\\]", "", mystring) # remove any backslashes
mystring = re.sub("\'", "\"", mystring) # replace single quotes with doubles
match = re.search(key_regex, mystring)
while match:
start_index = match.start(0)
end_index = match.end(0)
print(mystring[start_index+1:end_index-1].strip())
mystring = '%s"%s"%s'%(mystring[:start_index+1], mystring[start_index+1:end_index-1].strip(), mystring[end_index-1:])
match = re.search(key_regex, mystring)
return mystring