简体   繁体   中英

error in converting json to a dictionary in python

for i in products.iter():
    print i

This is the code I run and the output is as follows:

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'}

How do i make this become a python dict?

I tried making a list comprehension from the iterator used above and then doing json.loads(listcomprehension) but this gives me errors.

You start the list with a [ , okay, but then you end it with a ) . Change it to a ] . () are for tuples.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM