for p in db.collection.find({"test_set":"abc"}):
    posts.append(p)
thejson = json.dumps({'results':posts})
return  HttpResponse(thejson, mimetype="application/javascript")

In my Django/Python code, I can't return a JSON from a mongo query because of "ObjectID". The error says that "ObjectID" is not serializable.

我该怎么办?

for p in posts:
    p['_id'] = ""

推荐答案

The json module won't work due to things like the ObjectID.

幸运的是PyMongo提供了json_util...

... allow[s] for specialized encoding and decoding of BSON documents into Mongo Extended JSON's Strict mode. This lets you encode / decode BSON documents to JSON even when they use special BSON types.

Json相关问答推荐

PostgreSQL 12.17从JSON数组提取元素

从JSON格式提取数据时分隔通用名称

如何使用JQ有条件 Select 值

使用更高级别架构中的字段值在$def内实现约束

导致此 Kotlin Retrofit2 错误的可能原因有哪些?

如何在 jq 中按 IP 地址排序?

从 oracle 数据库中的 json blob 打印值

使用 Groovy 将 XML 转换为 JSON

为什么解析的字典相等而腌制的字典不相等?

使用 jq 和 awk 拆分大型 JSON 文件

如何在linux中用jq过滤json数组?

我无法在 Go - Gin 中解析日期/时间

使用 Ansible 解析来自 Juniper 交换机的 JSON 输出

Jolt - 在同一级别添加时组合值的问题

IE中Json响应下载(7~10)

以 unicode 将 pandas DataFrame 写入 JSON

消息通知产生此内容无法显示

JSON.stringify 不会转义?

将 Objective-C 对象序列化和反序列化为 JSON

如何从 github API 解析链接头