将json转换为python中的字典时出错

时间:2013-09-24 22:55:07

标签: python json dictionary

for i in products.iter():
    print i

这是我运行的代码,输出如下:

localhost:desktop siddharth$ python reader.py 
{u'sem3_id': u'1IngGhlIpoOGYCiMaEASSQ', u'cat_id': u'12151', u'updated_at': 1379589873, u'height': u'213.36', u'offers_total': 13, u'category': u'Inkjet Printers', u'price_currency': u'USD', u'sem3_help': u'To view image links for this product, please upgrade your plan.', u'width': u'281.94', u'sitedetails': [{u'sku': u'B00005AL05', u'latestoffers': [{u'price': u'366.99', u'shipping': u'19.98', u'seller': u'Buy4Less Now!', u'currency': u'USD', u'lastrecorded_at': 1379589800, u'firstrecorded_at': 1377106600, u'id': u'1BA5umTG80Gkg0oi40kiOu', u'condition': u'New'}, {u'price': u'379.96', u'shipping': u'12.49', u'seller': u'Battle Distribution, LLC', u'currency': u'USD', u'lastrecorded_at': 1379589800, u'firstrecorded_at': 1374975300, u'id': u'05cPhKOvouWkGc08sKaseq', u'condition': u'New'}, {u'price': u'99.99', u'shipping': u'12.49', u'seller': u'clabro', u'currency': u'USD', u'lastrecorded_at': 1379589800, u'firstrecorded_at': 1365164600, u'id': u'7aUtWYQlLkGuygGeWOgKWu', u'condition': u'Used - Very Good'}], u'name': u'amazon.com', u'url': u'http://www.amazon.com/dp/B00005AL05', u'offers_count': 13, u'recentoffers_count': 5}], u'price': u'366.99', u'gtins': [u'00033584113910'], u'ean': u'0033584113910', u'mpn': u'C408011', u'brand': u'Epson', u'geo': [u'usa'], u'manufacturer': u'Epson', u'name': u'Epson Stylus Photo 785EPX Inkjet Printer', u'created_at': 1347483414, u'upc': u'033584113910', u'length': u'264.16', u'model': u'C408011'}

我如何让这成为一个python dict?

我尝试从上面使用的迭代器中创建列表解析然后执行json.loads(listcomprehension),但这给了我错误。

1 个答案:

答案 0 :(得分:1)

你用[开始列表,好吧,但是你用)结束它。将其更改为]()用于元组。

>>> import json
>>> json.loads('[{"name":"same"}]')
[{u'name': u'same'}]