Python将字符串转换为字典

标签 python json 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)

关于Python将字符串转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46711204/

相关文章:

c - 尝试数据结构实现.........应用 - 字典

json - 将键列和多个值列聚合为 JSON 对象

android - Amazon SNS 控制台推送通知发送空通知

Python、 Selenium : Isolate Item From Returned List

python - 如何自动生成一两个词来代表一个主题?

c++ - 如何使用 QT 创建 JSON 数组

iphone - 显示 Plist 词典中的随机单词 - 内存泄漏?

python - 当真与当假

python - 文本(python)-如何在简单的文本对象中添加点击事件?

python - South/Django 问题 : not recognizing the Django App