簡體   English   中英

Python將字符串轉換為字典

[英]Python converting string to dictionary

我試圖使用JSON庫將此字符串轉換為字典。

string = {u'ItemID': u'474178239', u'Status': 1, u'ImageURL': u'https://img.shopstyle-cdn.com/pim/d2/1a/d21a5497da25891c11ce39e54a1171db_best.jpg', u'Last_Modified_Date': {u'$date': u'2017-08-05T05:28:42.773Z'}, u'ClientID': 6, u'Creation_Date': {u'$date': u'2017-08-05T05:28:42.773Z'}, u'ImageName': u'288216716fb441b5f07bc61b7fc827a9.jpg', u'_id': '55315426_03', u'CategoryID': u'18019', u'Metadata': {u'productPageURL': u'https://www.shopstyle.com/p/gianvito-rossi-vamp-100-suede-ankle-boots-black/474178239', u'price': u'895', u'description': u"<![CDATA[\n                        Cap off sophisticated evening looks with Gianvito Rossi's black ankle boots. Crafted in Italy from the finest suede , this almond-toe design has a low-cut, split vamp and extended topline. Wear yours with everything from dresses to tailored pants.\n                        ]]>", u'style': u'boots', u'title': u'Gianvito Rossi - Vamp 100 Suede Ankle Boots - Black'}}

該字符串絕對是字符串格式:

type(string)
str

但是,當我嘗試將其轉換為字典時使用:

dictionary = json.loads(string)

我明白了:

ValueError: Expecting property name: line 1 column 2 (char 1) 

無論如何我可以解決這個問題嗎?

提供的對象是字典。 但是,嘗試使用ast庫:

import ast
ast.literal_eval(string)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM