这是我第一次使用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