针对json.loads()报错的解决方案

str='''{\"count\":5,\"sub_images\":[{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/153118629842496112fc8be\",\"width\":640,\"url_list\":[{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/153118629842496112fc8be\"},{\"url\":\"http:\\/\\/pb9.pstatp.com\\/origin\\/pgc-image\\/153118629842496112fc8be\"},{\"url\":\"http:\\/\\/pb1.pstatp.com\\/origin\\/pgc-image\\/153118629842496112fc8be\"}],\"uri\":\"origin\\/pgc-image\\/153118629842496112fc8be\",\"height\":960},{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/15311863111893fba0fc97b\",\"width\":640,\"url_list\":[{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/15311863111893fba0fc97b\"},{\"url\":\"http:\\/\\/pb9.pstatp.com\\/origin\\/pgc-image\\/15311863111893fba0fc97b\"},{\"url\":\"http:\\/\\/pb1.pstatp.com\\/origin\\/pgc-image\\/15311863111893fba0fc97b\"}],\"uri\":\"origin\\/pgc-image\\/15311863111893fba0fc97b\",\"height\":959},{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/1531186304766ef8e818772\",\"width\":640,\"url_list\":[{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/1531186304766ef8e818772\"},{\"url\":\"http:\\/\\/pb9.pstatp.com\\/origin\\/pgc-image\\/1531186304766ef8e818772\"},{\"url\":\"http:\\/\\/pb1.pstatp.com\\/origin\\/pgc-image\\/1531186304766ef8e818772\"}],\"uri\":\"origin\\/pgc-image\\/1531186304766ef8e818772\",\"height\":1007},{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/153118629255722f4ffad5d\",\"width\":640,\"url_list\":[{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/153118629255722f4ffad5d\"},{\"url\":\"http:\\/\\/pb9.pstatp.com\\/origin\\/pgc-image\\/153118629255722f4ffad5d\"},{\"url\":\"http:\\/\\/pb1.pstatp.com\\/origin\\/pgc-image\\/153118629255722f4ffad5d\"}],\"uri\":\"origin\\/pgc-image\\/153118629255722f4ffad5d\",\"height\":937},{\"url\":\"http:\\/\\/p1.pstatp.com\\/origin\\/pgc-image\\/15311863179532decab8f79\",\"width\":640,\"url_list\":[{\"url\":\"http:\\/\\/p1.pstatp.com\\/origin\\/pgc-image\\/15311863179532decab8f79\"},{\"url\":\"http:\\/\\/pb3.pstatp.com\\/origin\\/pgc-image\\/15311863179532decab8f79\"},{\"url\":\"http:\\/\\/pb9.pstatp.com\\/origin\\/pgc-image\\/15311863179532decab8f79\"}],\"uri\":\"origin\\/pgc-image\\/15311863179532decab8f79\",\"height\":1024}],\"max_img_width\":640,\"labels\":[\"\\u6444\\u5f71\"],\"sub_abstracts\":[\"\\u8def\\u4eba\\u8857\\u62cd\\uff0c\\u8ddf\\u5bf9\\u8c61\\u4e00\\u8d77\\u62cd\\u7167\\uff0c\\u7a7f\\u7684\\u4e0d\\u591f\\u6f02\\u4eae\\u600e\\u4e48\\u80fd\\u884c\",\" \",\" \",\" \",\" \"],\"sub_titles\":[\"\\u8def\\u4eba\\u8857\\u62cd\\uff0c\\u8ddf\\u5bf9\\u8c61\\u4e00\\u8d77\\u62cd\\u7167\\uff0c\\u7a7f\\u7684\\u4e0d\\u591f\\u6f02\\u4eae\\u600e\\u4e48\\u80fd\\u884c\",\"\\u8def\\u4eba\\u8857\\u62cd\\uff0c\\u8ddf\\u5bf9\\u8c61\\u4e00\\u8d77\\u62cd\\u7167\\uff0c\\u7a7f\\u7684\\u4e0d\\u591f\\u6f02\\u4eae\\u600e\\u4e48\\u80fd\\u884c\",\"\\u8def\\u4eba\\u8857\\u62cd\\uff0c\\u8ddf\\u5bf9\\u8c61\\u4e00\\u8d77\\u62cd\\u7167\\uff0c\\u7a7f\\u7684\\u4e0d\\u591f\\u6f02\\u4eae\\u600e\\u4e48\\u80fd\\u884c\",\"\\u8def\\u4eba\\u8857\\u62cd\\uff0c\\u8ddf\\u5bf9\\u8c61\\u4e00\\u8d77\\u62cd\\u7167\\uff0c\\u7a7f\\u7684\\u4e0d\\u591f\\u6f02\\u4eae\\u600e\\u4e48\\u80fd\\u884c\",\"\\u8def\\u4eba\\u8857\\u62cd\\uff0c\\u8ddf\\u5bf9\\u8c61\\u4e00\\u8d77\\u62cd\\u7167\\uff0c\\u7a7f\\u7684\\u4e0d\\u591f\\u6f02\\u4eae\\u600e\\u4e48\\u80fd\\u884c\"]}
'''

当看到上述字符串,如果想获得url的值,自然而然想到的是用 json.loads(str) 将字符串转换成json类型,这时因为loads()能够在把字符串转换成json类型时自动去掉\,转换后结果如下:

str_='''{'count': 5, 'sub_images': [{'url': 'http://p3.pstatp.com/origin/pgc-image/153118629842496112fc8be', 'width': 640, 'url_list': [{'url': 'http://p3.pstatp.com/origin/pgc-image/153118629842496112fc8be'}, {'url': 'http://pb9.pstatp.com/origin/pgc-image/153118629842496112fc8be'}, {'url': 'http://pb1.pstatp.com/origin/pgc-image/153118629842496112fc8be'}], 'uri': 'origin/pgc-image/153118629842496112fc8be', 'height': 960}, {'url': 'http://p3.pstatp.com/origin/pgc-image/15311863111893fba0fc97b', 'width': 640, 'url_list': [{'url': 'http://p3.pstatp.com/origin/pgc-image/15311863111893fba0fc97b'}, {'url': 'http://pb9.pstatp.com/origin/pgc-image/15311863111893fba0fc97b'}, {'url': 'http://pb1.pstatp.com/origin/pgc-image/15311863111893fba0fc97b'}], 'uri': 'origin/pgc-image/15311863111893fba0fc97b', 'height': 959}, {'url': 'http://p3.pstatp.com/origin/pgc-image/1531186304766ef8e818772', 'width': 640, 'url_list': [{'url': 'http://p3.pstatp.com/origin/pgc-image/1531186304766ef8e818772'}, {'url': 'http://pb9.pstatp.com/origin/pgc-image/1531186304766ef8e818772'}, {'url': 'http://pb1.pstatp.com/origin/pgc-image/1531186304766ef8e818772'}], 'uri': 'origin/pgc-image/1531186304766ef8e818772', 'height': 1007}, {'url': 'http://p3.pstatp.com/origin/pgc-image/153118629255722f4ffad5d', 'width': 640, 'url_list': [{'url': 'http://p3.pstatp.com/origin/pgc-image/153118629255722f4ffad5d'}, {'url': 'http://pb9.pstatp.com/origin/pgc-image/153118629255722f4ffad5d'}, {'url': 'http://pb1.pstatp.com/origin/pgc-image/153118629255722f4ffad5d'}], 'uri': 'origin/pgc-image/153118629255722f4ffad5d', 'height': 937}, {'url': 'http://p1.pstatp.com/origin/pgc-image/15311863179532decab8f79', 'width': 640, 'url_list': [{'url': 'http://p1.pstatp.com/origin/pgc-image/15311863179532decab8f79'}, {'url': 'http://pb3.pstatp.com/origin/pgc-image/15311863179532decab8f79'}, {'url': 'http://pb9.pstatp.com/origin/pgc-image/15311863179532decab8f79'}], 'uri': 'origin/pgc-image/15311863179532decab8f79', 'height': 1024}], 'max_img_width': 640, 'labels': ['摄影'], 'sub_abstracts': ['路人街拍,跟对象一起拍照,穿的不够漂亮怎么能行', ' ', ' ', ' ', ' '], 'sub_titles': ['路人街拍,跟对象一起拍照,穿的不够漂亮怎么能行', '路人街拍,跟对象一起拍照,穿的不够漂亮怎么能行', '路人街拍,跟对象一起拍照,穿的不够漂亮怎么能行', '路人街拍,跟对象一起拍照,穿的不够漂亮怎么能行', '路人街拍,跟对象一起拍照,穿的不够漂亮怎么能行']}
'''

然而有时转换过程中会出现错误:

'dict' object has no attribute 'loads'

而且这种错误是通过增加encoding='utf-8'后所不能解决的。

这时,我们可以用eval(str)函数将字符串转换成字典类型,代码如下:

m=str.replace('\\','')  #去除str中的\
str_=eval(m)

 

猜你喜欢

转载自blog.csdn.net/baidu_20965275/article/details/80998883
今日推荐