简体   繁体   中英

List of lists of objects to JSON with Python

I have a List with multi lists of objects:

ob = Obj()
for p in l:
    list_url_commune = scrapingCities(base_url, p)
    for u in list_url_commune:
        list_name = scaping_creches(base_url, u, number_page)
        list_dep.append(list_name)

 json_string = json.dumps([ob.__dict__ for ob in list_dep])
 print json_string

list_dep is the list with all the lists with Objects

Then, I would like to convert all the Objects in only one JSON .

In my case, I have an error with json.dumps([ob.__dict__ for ob in list_dep])

(notice: it works fine if I have only one list but here it is a List of lists)

AttributeError: 'list' object has no attribute '__dict__'

Thanks for your help !

Pedro

Just like you noticed yourself, it works fine with a list of objects, but not with a list of lists of objects. You need to go deeper:

json.dumps([[ob.__dict__ for ob in lst] for lst in list_dep])

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