2017-10-12 242 views
1

我想將此字符串轉換爲使用JSON庫的字典。Python將字符串轉換爲字典

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) 

無論如何,我可以得到AR回答這個?

+2

您變量'string'提供的代碼是不是_String_這是字典。 –

+0

所以,你的值是''string = {u'...''(Python代碼的一個片段作爲字符串)還是''{u'...「'(一個Python字典字符串作爲字符串)或' {u'...'(字典)?這是相當模糊的。 – deceze

回答

3

提供的對象是一個字典。 然而, 嘗試使用AST庫:

import ast 
ast.literal_eval(string)