我有一个包含json文本的字符串。 我把它转换成原始字符串
json_raw = "%r"%json_all
然后我试着加载它
json_dict = json.loads(json_raw)
给我错误JSONDecodeError: Expecting value: line 1 column 1 (char 0)
为了调试,我做了print(json_raw)
,控制台输出给我:
'{... omitted ...}'
我不知道我是怎么得到额外的单引号的。我的代码现在看起来像这样
json_all = "{"+json2+"}"
json_raw = "%r"%json_all
json_dict = json.loads(json_raw)
我查过https://jsonformatter.curiousconcept.com
好像我的线没有正确地用Scrapy转义。你知道吗
使用Scrapy Shell
检查
scrapy shell "https://www.alibabacloud.com/pricing"
json_response = response.xpath('//*[@id="floor1"]/textarea').extract()
用Scrapy转义的方式破坏了json的原始正确格式。如果我复制粘贴original html的json,则没有错误。你知道吗
我把零件拆了,好像惹麻烦了。你知道吗
d_str = '* Each package includes an outbound Data Transfer plan. Any outbound data transfers in excess of your plan\'s Data Transfer allowance are subject to overage charges. <a href="https://www.alibabacloud.com/starter-packages/general" class=\'\\"show-intl\\"\'>Learn more</a><br>*'
json_new = json_response[0].replace(d_str,'',1)
# split on the first character and take the last element of the result
json1=json_new.split("{",1)[-1]
json2=json1.rsplit("}",1)[0]
json_all = '{'+json2+'}'
json_dict = json.loads(json_all)
现在我可以装了。你知道吗
你不应该把你的字符串括在引号里,如下一行。你知道吗
错误显示意味着第一个字符无效,由引号字符标识。对于json,第一个字符以{或[
使用jsonformatter.curiousconcept.com检查json并跟踪报告。你知道吗
在使用
"
包装键和值时,应该正确地转义"
。你知道吗更正后的json应为:
相关问题 更多 >
编程相关推荐