首页 > 解决方案 > 带有烧瓶的 rets-python 不能作为 json 返回

问题描述

我正在做一个与 相关的项目rets,不知何故我的合作者在安装过程中使用rets-clientfor有问题。js无论如何,所以我决定找到pythonrets-pythonhttps://github.com/opendoor-labs/rets找到的

我能够从返回的数据rets,不知何故我想做一个api所以我安装了flask(新的烧瓶)

但是我试图将数据返回到JSON我只是不断收到错误

我这是我的代码

import json

from flask import Flask, Response, jsonify

app = Flask(__name__)


@app.route('/')
def mls():
    from rets.client import RetsClient

    client = RetsClient(
        login_url='xxxxxxxxxxxx',
        username='xxxxxxxxxxxx',
        password='xxxxxxxxxxxxx',
        # Ensure that you are using the right auth_type for this particular MLS
        # auth_type='basic',
        # Alternatively authenticate using user agent password
        user_agent='xxxxxxxxxxxxxxxx',
        user_agent_password='xxxxxxxxxxxxxxxx'
    )

    resource = client.get_resource('Property')
    print(resource)

    resource.key_field

    print(resource.key_field)

    resource_class = resource.get_class('RD_1')
    print(resource_class)

    search_result = resource_class.search(query='(L_Status=1_0)', limit=2)

我将在这里写下我的回报,因为我尝试了几种不同的回报

    # return jsonify(results=search_result.data)
    # TypeError: <Record: Property:RD_1:259957852> is not JSON serializable

    return Response(json.dumps(search_result.data), mimetype='application/json')
    # TypeError: <Record: Property:RD_1:259957852> is not JSON serializable

    return json.dumps(search_result.data)
    # TypeError: <Record: Property:RD_1:259957852> is not JSON serializable

我尝试将结果放入 dict 中,例如dict(search_result.data)这给了我类似的错误TypeError: cannot convert dictionary update sequence element #0 to a sequence

如果我试图得到这是 我试图得到print(type(search_result.data[0].data))的回报,而且无论有没有这一条记录,我都会收到此错误<class 'collections.OrderedDict'>jsonifyjson.dumpResponsedict()TypeError: Decimal('1152') is not JSON serializable

print(type(search_result.data))tuple

任何人都可以帮我解决这个问题吗?

提前感谢您的任何帮助和建议。

标签: pythonjsonflaskrets

解决方案


推荐阅读