Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x TypeError:类型为'的对象;地点';JSON不可序列化_Python 3.x_Mongodb_Flask_Geopy_Flask Pymongo - Fatal编程技术网

Python 3.x TypeError:类型为'的对象;地点';JSON不可序列化

Python 3.x TypeError:类型为'的对象;地点';JSON不可序列化,python-3.x,mongodb,flask,geopy,flask-pymongo,Python 3.x,Mongodb,Flask,Geopy,Flask Pymongo,我正在为我的Flask web应用程序使用geopy库。我想将从我的模式(html表单)中获得的用户位置保存到数据库中(我使用的是mongodb),但每次出现此错误时: TypeError:Location类型的对象不可JSON序列化 代码如下: @app.route('/register', methods=['GET', 'POST']) def register_user(): if request.method == 'POST': login_user = mo

我正在为我的Flask web应用程序使用geopy库。我想将从我的模式(html表单)中获得的用户位置保存到数据库中(我使用的是mongodb),但每次出现此错误时: TypeError:Location类型的对象不可JSON序列化

代码如下:

@app.route('/register', methods=['GET', 'POST'])
def register_user():
    if request.method == 'POST':
        login_user = mongo.db.mylogin
        existing_user = login_user.find_one({'email': request.form['email']})
        # final_location = geolocator.geocode(session['address'].encode('utf-8'))
        if existing_user is None:
            hashpass = bcrypt.hashpw(
                request.form['pass'].encode('utf-8'), bcrypt.gensalt())
            login_user.insert({'name': request.form['username'], 'email': request.form['email'], 'password': hashpass, 'address': request.form['add'], 'location' : session['location'] })
            session['password'] = request.form['pass']
            session['username'] = request.form['username']
            session['address'] = request.form['add']
            session['location'] = geolocator.geocode(session['address'])
            flash(f"You are Registerd as {session['username']}")
            return redirect(url_for('home'))
        flash('Username is taken !')
        return redirect(url_for('home'))
    return render_template('index.html')
请提供帮助,如果您需要更多信息,请告诉我。

根据地理编码功能“按地址返回位置点”对象集

默认情况下,Json序列化支持以下类型:

Python | JSON

dict | object

列表,元组|数组

str,unicode |字符串

整型,长型,浮点数

对|对

假|假

无|空

默认情况下,所有其他对象/类型都不是json序列化的,因此需要对其进行定义

位置的原始、未分析的地理编码器响应。有关详情,, 请查阅服务文档

返回类型:dict或None

您可能可以调用Location的原始函数(geoloator.geocode返回值),该值将是json可序列化的

确实不是json可序列化的:这个对象中有许多属性,并且没有单一的方法来表示位置,所以您必须自己选择一个

您希望在响应的
位置
键中看到什么类型的值

以下是一些例子:

文本地址 点坐标 原始提名响应 (这可能不是您希望在API中公开的内容,假设您希望保留将来将地理编码服务更改为另一个服务的能力,该服务可能具有不同的
raw
响应模式)

文本地址+点坐标
In [9]: json.dumps({'location': geolocator.geocode("175 5th Avenue NYC").address})
Out[9]: '{"location": "Flatiron Building, 175, 5th Avenue, Flatiron District, Manhattan Community Board 5, Manhattan, New York County, New York, 10010, United States of America"}'
In [10]: json.dumps({'location': list(geolocator.geocode("175 5th Avenue NYC").point)})
Out[10]: '{"location": [40.7410861, -73.9896298241625, 0.0]}'
In [11]: json.dumps({'location': geolocator.geocode("175 5th Avenue NYC").raw})
Out[11]: '{"location": {"place_id": 138642704, "licence": "Data \\u00a9 OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright", "osm_type": "way", "osm_id": 264768896, "boundingbox": ["40.7407597", "40.7413004", "-73.9898715", "-73.9895014"], "lat": "40.7410861", "lon": "-73.9896298241625", "display_name": "Flatiron Building, 175, 5th Avenue, Flatiron District, Manhattan Community Board 5, Manhattan, New York County, New York, 10010, United States of America", "class": "tourism", "type": "attraction", "importance": 0.74059885426854, "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_point_of_interest.p.20.png"}}'
In [12]: location = geolocator.geocode("175 5th Avenue NYC")
    ...: json.dumps({'location': {
    ...:     'address': location.address,
    ...:     'point': list(location.point),
    ...: }})
Out[12]: '{"location": {"address": "Flatiron Building, 175, 5th Avenue, Flatiron District, Manhattan Community Board 5, Manhattan, New York County, New York, 10010, United States of America", "point": [40.7410861, -73.9896298241625, 0.0]}}'