繁体   English   中英

Python中无效的JSON到字典

[英]Invalid JSON to Dictionary in Python

我正在使用scrapy抓取网站上的数据。 该网站给了我JSON,并且还在JSON值中添加了HTML标记,这破坏了JSON。 我正在使用此函数json.loads(json)将json转换为python中的dict。 但这给了我无效的json错误。 我看一下JSON,由于HTML的引号" ,它无效。示例如下。

{"header":"<h2>Clothing</h2>\r\n\t\t\t\t\t<div class="count-text">\r\n\t\t\t\t 438 styles found</div>","refinements":"<div id="product-facet" class="col-md-3 col-sm-6 filter-box top-box js-product-facet">\r\n<div class="filter-heading">\r\n<span>Refine</span><span class="clear"><a href="?text=&amp;showType=&amp;sort=">Clear all</a></span></div>\t\r\n<div class="filter-container">\r\n\t\t<div class="filter-label main-label ">Refine<a href="javascript:void(0);" data="facets-container" class="close">"}

如您所见,在json值中有一些"使其无效。我如何将这种JSON类型转换为python中的dict?

无效的JSON可能是由错误的生成器生成的。 这似乎应该更普遍。

有一个工具,称为dirty-json ,可能会有帮助:

https://github.com/RyanMarcus/dirty-json

还有一个在线版本 无效的JSON的解释如下:

{
    "header": "<h2>Clothing</h2>\r\n\t\t\t\t\t<div class=\"count-text\">\r\n\t\t\t\t 438 styles found</div>",
    "refinements": "<div id=\"product-facet\" class=\"col-md-3col-sm-6filter-boxtop-boxjs-product-facet\">\r\n<div class=\"filter-heading\">\r\n<span>Refine</span><span class=\"clear\"><a href=\"?text=&amp;showType=&amp;sort=\">Clear all</a></span></div>\t\r\n<div class=\"filter-container\">\r\n\t\t<div class=\"filter-labelmain-label\">Refine<a href=",
    "javascript": "void(0); data=\"facets-container\" class=\"close\">"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM