2017-10-12 247 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)