gpt4 book ai didi

python - GeoPoint 类型的对象不是 JSON 可序列化的

转载 作者:行者123 更新时间:2023-11-28 19:28:40 25 4
gpt4 key购买 nike

我想创建一个 API,它必须从 firestore 恢复数据并将它们公开在 json 中,但我遇到了以下错误:

Object of type GeoPoint is not JSON serializable

怎么了?这是我的代码:

#!/usr/bin/env python
import firebase_admin
import json
from pprint import pprint as pp
from flask import Flask, flash, request, url_for, jsonify, Response
from firebase_admin import credentials
from firebase_admin import firestore

app = Flask(__name__)

cred = credentials.Certificate('xxxx.json')
firebase_admin.initialize_app(cred)
db = firestore.client()

@app.route('/')
def index():

markers_ref = db.collection(u'Test')
docs = markers_ref.get()

data = []
for doc in docs:
data.append(doc.to_dict())

json_data = json.dumps(data)
resp = Response(json_data, status=200, mimetype='application/json')

return resp

if __name__=='__main__':
app.run(debug=True)

最佳答案

以下应该可以解决问题 -

from flask import (Flask, request, session, g, redirect, url_for, abort, render_template, flash, Response, jsonify)
import firebase_admin
from firebase_admin import firestore

@app.route('/foods/<id>')
def read_food(id):

result=FOODS.document(id).get().to_dict()
record = {}
# Populate the new dict with JSON serializiable values
for key in result:
if isinstance(result[key], firebase_admin.firestore.GeoPoint):
record[key] =str(result[key].latitude)+','+str(result[key].longitude)
continue
record[key] = result[key]

# Add the key so that we have a reference to the record
record['key'] = result[key]

return jsonify(record)

关于python - GeoPoint 类型的对象不是 JSON 可序列化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54833700/

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