从clien接收数据时匹配JSON模式

2024-10-03 17:27:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个flaskrest实现来接收以下数据。你知道吗

在从客户机检查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"
      ]   
  ] 
  }
}

我的代码如下,如果字段名“services”有一个字符串,就像其他字符串一样(如“id”、“termsAndConditions”等)。你知道吗

from flask_pymongo import PyMongo
import json
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'demo'
app.config['MONGO_URI'] = 'mongodb://xxxx@xxxx.mlab.com: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”下。你知道吗


Tags: 数据key字符串apiidjsonappstring
1条回答
网友
1楼 · 发布于 2024-10-03 17:27:16

您可以使用isinstance(“str”,请求.json[“服务”])

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

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

相关问题 更多 >