1. 程式人生 > 實用技巧 >python3 處理字典中存在反斜槓,將其轉換為字典

python3 處理字典中存在反斜槓,將其轉換為字典

code

a={
"breaker": [{"body":"{\"request_id\":\"403\",\"code\":0,\"type\":0,\"msg\":\"success\",\"data\":{\"banner\":[{\"id\":245,\"name\":\"蘋果手機專區\",\"title\":\"蘋果手機專區\",\"image_src\":\"https://image.fenbeitong.com/banner/2020/10/23/138244/5f92b16d68195b6be07a235e.jpg\",\"target_type\":5,\"target_url\":\"\",\"target_id\":34,\"target_page_busi_type\":2},{\"id\":252,\"name\":\"美妝專區\",\"title\":\"美妝專區\",\"image_src\":\"https://image.fenbeitong.com/banner/2020/10/29/138246/5f9a72a2d40cd74f1b53656a.jpg\",\"target_type\":5,\"target_url\":\"\",\"target_id\":35,\"target_page_busi_type\":2},{\"id\":240,\"name\":\"秋意濃\",\"title\":\"秋意濃\",\"image_src\":\"https://image.fenbeitong.com/banner/2020/10/19/138428/5f8d8ac768195b153b8e0aca.jpg\",\"target_type\":5,\"target_url\":\"\",\"target_id\":33,\"target_page_busi_type\":2}],\"hot_category\":[{\"id\":16,\"name\":\"時尚\",\"title\":\"時尚\",\"image_src\":\"http://image.fenbeitong.com/banner/2019/04/02/138246/5ca2c9d45d88db2bf37957c5.png\",\"target_type\":3,\"target_url\":\"\",\"target_id\":1425,\"target_page_busi_type\":2},{\"id\":18,\"name\":\"美食\",\"title\":\"美食\",\"image_src\":\"http://image.fenbeitong.com/banner/2019/04/02/138246/5ca2c9fc5f281a73176d3e55.png\",\"target_type\":3,\"target_url\":\"\",\"target_id\":5020,\"target_page_busi_type\":2},{\"id\":17,\"name\":\"數碼\",\"title\":\"數碼\",\"image_src\":\"http://image.fenbeitong.com/banner/2019/04/02/138246/5ca2ca0c5f281a73176d3e57.png\",\"target_type\":3,\"target_url\":\"\",\"target_id\":831,\"target_page_busi_type\":2},{\"id\":19,\"name\":\"居家\",\"title\":\"居家\",\"image_src\":\"http://image.fenbeitong.com/banner/2019/04/02/138246/5ca2ca6f5f281a73176d3e58.png\",\"target_type\":3,\"target_url\":\"\",\"target_id\":15251,\"target_page_busi_type\":2},{\"id\":20,\"name\":\"全部分類\",\"title\":\"全部分類\",\"image_src\":\"http://image.fenbeitong.com/banner/2019/04/02/138246/5ca2ca8b5f281a73176d3e5b.png\",\"target_type\":4,\"target_url\":\"\",\"target_page_busi_type\":2}],\"channel\":[{\"id\":204,\"name\":\"9.9元專區\",\"title\":\"9.9元專區\",\"image_src\":\"http://image.fenbeitong.com/banner/2020/07/08/138246/5f055d9c5f281a2218698c4f.jpg\",\"target_type\":5,\"target_url\":\"\",\"target_id\":27,\"target_page_busi_type\":2},{\"id\":167,\"name\":\"樊登讀書\",\"title\":\"樊登讀書\",\"image_src\":\"http://image.fenbeitong.com/banner/2020/07/28/138244/5f1fc02706a5c303fb364d1c.jpg\",\"target_type\":5,\"target_url\":\"\",\"target_id\":24,\"target_page_busi_type\":2},{\"id\":213,\"name\":\"精選下午茶\",\"title\":\"精選下午茶\",\"image_src\":\"http://image.fenbeitong.com/banner/2020/07/09/138246/5f06761106a5c303fb363ec5.jpg\",\"target_type\":5,\"target_url\":\"\",\"target_id\":28,\"target_page_busi_type\":2},{\"id\":214,\"name\":\"網紅熱賣榜\",\"title\":\"網紅熱賣榜\",\"image_src\":\"http://image.fenbeitong.com/banner/2020/07/18/138244/5f128fc75f281a2218699370.png\",\"target_type\":5,\"target_url\":\"\",\"target_id\":29,\"target_page_busi_type\":2}]}}
","createTime":1606825737000,"domainId":"5fb34615de2b840decb4b7da","id":6,"name":"因私-首頁輪播圖","orderNo":1606825736816,"updateTime":1606825737000,"uri":"/stereo/open/config/private/mall/index"}] }

安裝:

pip install demjson

code

import demjson
change_dic=demjson.decode(a)