gpt4 book ai didi

python : JSON can't be serialized

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:22 25 4
gpt4 key购买 nike

我有以下 json:

{
u'objectid': u'427912',
u'fooditems': u'Cold Truck: Hamburger: cheeseburgers: hot dogs: hot sandwiches: cold sandwiches: egg muffins: cup of noodles: corn dogs: canned soup: coffee: hot cocoa: hot tea: gatorade: juice: milk: soda: water: fruits: fruit salad: rice pudding: yogurt: candy bars: chips: cookies: donuts: granola bars: muffins',
u'facilitytype': u'Truck',
u'priorpermit': u'1',
u'location': {
u'latitude': u'37.730906164188',
u'needs_recoding': False,
u'longitude': u'-122.373302577475'
},
u'lot': u'008',
u'cnn': u'7253000',
u'status': u'APPROVED',
u'schedule': u'http://bsm.sfdpw.org/PermitsTracker/reports/report.aspx?title=schedule&report=rptSchedule&params=permit=13MFF-0072&ExportPDF=1&Filename=13MFF-0072_schedule.pdf',
u'locationdescription': u'INNES AVE: EARL ST to ARELIOUS WALKER DR (700 - 799)',
u'latitude': u'37.7309061503597',
u'blocklot': u'4644008',
u'address': u'Assessors Block 4644/Lot008',
u'approved': u'2013-04-04T08:44:08',
u'received': u'Mar 15 2013 10:24AM',
u'applicant': u"Park's Catering",
u'longitude': u'-122.373302577485',
u'expirationdate': u'2014-03-15T00:00:00',
u'permit': u'13MFF-0072',
u'y': u'2094023.408',
u'x': u'6019956.89',
u'_id': ObjectId('52afeb27e8de3f3174110041'),
u'block': u'4644'
}

当我对其调用 json.dumps() 时,出现错误 raise TypeError(repr(o) + "is not JSON serializable")

我做错了什么?

最佳答案

您在字典中包含一个 ObjectId() 对象。

如果没有特殊处理,该对象是不可序列化的。用原始值替换它,或者为 default 参数提供一个函数来为您编码此类对象:

def objectid_default(obj):
if isinstance(obj, ObjectId):
return str(obj) # hex string version
raise TypeError(obj)

json.dumps(d, default=objectid_default)

关于 python : JSON can't be serialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20642684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com