나는 Flask 및 Mongo DB를 사용하여 Rest API를 구축하고 있습니다.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})
나는 하드 데이터가 단정하고 잘 포맷 보여주는 일 아닌가요 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()'와 한 번,'jsonify()'로 한 번. –
만약 내가 jsonify를 사용하는 경우에만 오류가 발생합니다 :는 JSON을 직렬화 할 수 없으며 만약 덤프에주는 에러가 있다면 : TypeError : 'dict'객체를 호출 할 수 없습니다 –