python - 我在测试 Google Cloud Function 时遇到问题
问题描述
这是我第一次使用 Google Cloud Functions。而且我无法运行该功能。我的代码如下所示。
import json
def location_sort(request):
request = json.dumps(request)
location = json.loads(request)
reverse_location = {v: k for k, v in location.items()}
x = location.keys()
harf_x = (float(max(x)) + float(min(x))) / 2
y_right = []
y_left = []
sorted_location = []
for i in location:
if float(i) < harf_x:
y_left.append(location[i])
else:
y_right.append(location[i])
y_left.sort()
y_right.sort(reverse=True)
sorted_input = y_left + y_right
for i in sorted_input:
sorted_location.append([reverse_location[i], i])
return json.dumps(sorted_location)
我把触发器
{"38:127":"127.012","37.128":"127.002"}
并且错误显示为
Error: function terminated. Recommended action: inspect logs for termination reason. Details:
Object of type LocalProxy is not JSON serializable
我该如何解决我的问题?
解决方案
这里的request
对象对应于 Flask 的Request
对象,这意味着您可以从请求中获取 JSON,而不是尝试转储/加载request
对象本身:
location = request.get_json()
更多细节在这里:https ://flask.palletsprojects.com/en/1.1.x/api/#flask.Request.get_json
推荐阅读
- r - 关于 R 中 rpart 和 gbm 的一些问题
- apache - AH00558:httpd:无法可靠地确定服务器的完全合格
- python - 是否可以添加项目符号以绘制在 jupyter 幻灯片中呈现的输出?
- javascript - 如果我已经定义了 ID,如何将数组插入到我的 MongoDB 缝合中?
- python - 意外中断的 cProfile
- graph - 使用 cytoscape 分配节点坐标
- sql - 运行具有相互依赖关系的 sql 脚本
- javascript - 获取时如何从 API 获取编号的 ID?
- javascript - 视频标签不会在 Firefox 中自动播放,DOMException
- sharepoint - SharePoint 使用另一个列表中的数据计算列文本?