私は、Rest APIを構築するためにFlaskとMongo DBを使用しています。pymongo FlaskからJsonを検証する
pymongoを使用してMongoDBから取得しているデータが有効なJSONであるかどうかわかりません。
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.route('/tankover/api/v1.0/posts', methods=['GET'])
def post():
db = connection.posthub
cursor = dumps(db.post.find())
return jsonify({'cursor': cursor})
私はハードコーディングされたデータがきちんとしても書式設定示すis't jsonify CODE。
OUTPUT
{
"cursor": [
{
"description": "Milk, Cheese, Pizza, Fruit, Tylenol",
"done": false,
"id": 1,
"title": "Buy groceries"
},
{
"description": "Need to find a good Python tutorial on the web",
"done": false,
"id": 2,
"title": "Learn Python"
}
]
}
しかし、私は、データベースからのデータを使用しています。私はタイプとフォーマットが分からない。
{
"cursor": "[{\"title\": \"sankit\", \"_id\"
{\"$oid\":\"597619b7c07b2dc30a108def\"}, \"description\": \"hello to
everyone we are up
for a great start and moving good\", \"tags\": [\"demo1\", \"demo2\"]},
{\"_id\": {\"$oid\": \"59761b2cc6568a4e341b6b89\"}, \"description\": \"lets
add some thing new\", \"tags\": [\"bonjour\", \"salut\"], \"title\":
\"hi\"},
{\"_id\": {\"$oid\": \"59a5c5f6c6568a0be4447dfb\"}, \"description\": \"okay
okay okay\", \"tags\": [\"socks\", \"gifts\"], \"title\": \"tinni\"}]"
}
有効で正常ですか?
あなたは、二重エンコードされていますデータは 'dumps()'で1回、 'jsonify()'で1回です。 –
私はjsonifyの授受エラーのみを使用している場合:はJSONのシリアライズ可能ではなく、唯一のダンプを使用している場合:TypeError: 'dict'オブジェクトは呼び出し可能ではありません –