2017-05-24 44 views
-1

我编写了一个Flask REST实现来接收以下数据。从客户端接收数据时匹配JSON模式

从客户端检查API密钥后,服务器应该存储来自以下API定义的数据。我面临的问题是,我在相同的领域'服务'下有很多字符串,我会很感激任何帮助。

{ 
    "id": "string", 
    "termsAndConditions": "string", 
    "offererBranchId": "string", 
    "requesterBranchId": "string", 
    "accepted": "2017-05-24T10:06:31.012Z", 
    "services": [ 
    { 
     "id": "string", 
     "name": "string", 
     "aggregationLevel": [ 
     "string" 
     ], 
     "aggregationMethod": [ 
     "string" 
     ], 
     "timestep": [ 
     "string" 
     ] 
    ] 
    } 
} 

我的代码如下,如果字段名为“服务”有一个单一的字符串,它像其他的人(即“ID”,“termsAndConditions”等)。

from flask_pymongo import PyMongo 
import json 
app = Flask(__name__) 
app.config['MONGO_DBNAME'] = 'demo' 
app.config['MONGO_URI'] = 'mongodb://[email protected]:xxxx/demo' 
mongo = PyMongo(app) 
users = mongo.db.users 
@app.route('/service-offer/confirmed/REQUESTER',methods=['POST']) 

def serviceofferconfirmed(): 
    key = request.headers.get('X-API-Key') 

    users=mongo.db.users 
    api_record=users.find_one({'name':"apikey"}) 
    actual_API_key=api_record['X-API-Key'] 
    if key==actual_API_key: 
     offer={"id": request.json["id"], 
      "termsAndConditions":request.json["termsAndConditions"], 
      "offererBranchId":request.json["offererBranchId"], 
      "requesterBranchId": request.json["requesterBranchId"], 
      "accepted":request.json["accepted"], 
      "services":request.json["services"] # Here I need help to match the schema. 
      } 
     users.insert(offer) 
     return "Service Data Successfully Stored" 
    return jsonify("Pleae check your API Key or URL") 

我希望接收整个数据是多个字符串并将数据存储在字段名称'services'下。

回答

1

你可以使用isinstance( “海峡”,request.json [ “服务”])

,如果你不希望值作为字符串服务

if not isinstance("str", request.json["services"]): 
    // your code..........