有什么解决方法可以轻松验证python中的嵌套JSON响应吗?
在flask restplus中,我使用有什么解决方法可以轻松验证python中的嵌套JSON响应吗?,python,json,validation,flask,Python,Json,Validation,Flask,在flask restplus中,我使用api.modeldecorator定义了响应体数据结构,我希望api函数的输出应该产生我定义的精确数据结构。我不知道该怎么做。有人能告诉我如何验证flask中的json响应内容吗?使用定义的模式验证json响应输出的任何解决方法?有什么想法吗 电流输出: 下面是我向api函数发出POST请求后的当前输出:myfunc: "{\n\r\n \"score\": [72.188, 62.0955, 19.3374, 45.6086, 77.8891, 22
api.model
decorator定义了响应体数据结构,我希望api函数的输出应该产生我定义的精确数据结构。我不知道该怎么做。有人能告诉我如何验证flask中的json响应内容吗?使用定义的模式验证json响应输出的任何解决方法?有什么想法吗
电流输出:
下面是我向api函数发出POST请求后的当前输出:myfunc
:
"{\n\r\n \"score\": [72.188, 62.0955, 19.3374, 45.6086, 77.8891, 22.188, 45.9938, 91.9877, 14.2527, 1.5408, 62.5578],\n\r\n \"category\": \"low\",\n\r\n \"direction\": \"text description\",\n\r\n \"is_ready\": true,\n\r\n \"features_used\": {\n\r\n \"name\": \"heart_rate\",\n\r\n \"value\": null,\n\r\n \"range_value\": [3.6667, 5, 6.3333, 7.6667, 9, 10.3333, 11.6667, 13, 14.3333],\n\r\n \"range_frequency\": [0.0024, 0, 0.0049, 0.0016, 0.0073, 0.0195, 0.0098, 0.0138, 0.9406],\n\r\n \"level\": 0\n\r\n }\n\r\n} \n"
问题是当前输出的格式并没有产生主体定义的响应。如何解决这个问题?如何验证flask中的json响应内容?有什么可能的方法可以做到这一点吗?谢谢
具有指定响应json正文的最小api
from flask import Flask, jsonify
from flask_restplus import Namespace, Resource, fields, reqparse
from flask_restplus import Api
app = Flask(__name__)
api = Api(app)
ns = api.namespace('hello-world')
used_features = {}
used_features['name'] = fields.String(attribute='name')
used_features['value'] = fields.Integer(attribute='value')
used_features['range_value'] = fields.List(
fields.Integer, attribute='range_value')
used_features['range_frequency'] = fields.List(
fields.Integer, attribute='range_frequency')
used_features['level'] = fields.Integer(attribute='level')
used_features_payload = api.model('feature_payload', used_features)
response_body= api.model('response', {
'score': fields.Integer,
'category': fields.String,
'direction': fields.String,
'is_ready': fields.Boolean,
'features_used': fields.Nested(used_features_payload)
})
目标:
我想验证当前输出的JSON模式。如何使用响应体JSON模式验证函数输出?有什么想法吗?忽略代码中的技术细节 要验证JSONschema或任何定义的模式,可以使用包 这个软件包使用起来相当简单。 在这里引用用法的一部分
schema = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
},
}
您可以这样定义模式,只需传递对象以根据该模式进行验证。如果不匹配,验证程序将抛出异常
from jsonschema import validate
validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)
你可以用棉花糖来验证你的反应。我发现你的问题如下:
from marshmallow import Schema, fields, post_load
from marshmallow import EXCLUDE
import json
from flask import jsonify
class Feature:
def __init__(self, name, value, range_value, range_frequency, importance):
self.name = name
self.value = value
self.range_value = range_value
self.range_frequency = range_frequency
self.importance = importance
class FeatureSchema(Schema):
value = fields.Integer()
name = fields.Str()
importance = fields.Integer()
range_value = fields.List(fields.Integer)
range_frequency = fields.List(fields.Integer)
@post_load
def make_feature(self, data, **kwargs):
return Feature(**data)
class App(object):
def __init__(self, score, category, guidance, readiness, features_used):
self.score = score
self.category = category
self.guidance = guidance
self.readiness = readiness
self.features_used = features_used
class RespSchema(Schema):
score = fields.Integer()
category = fields.Str()
guidance = fields.Str()
readiness = fields.Boolean()
features_used = fields.List(fields.Nested(FeatureSchema))
@post_load
def make_app(self, data, **kwargs):
return App(**data)
棉花糖在数据验证方面非常有效。您的
post
方法正在字符串化变量res
,您是否希望返回jsonify(res)
?谢谢,但是如果您遵循我的上述尝试,您能否详细说明您的答案?你的方法对我的尝试有何作用?还有什么想法吗?thankshow您的尝试对我的响应体模式有效吗?如何使用jsonschema
验证我的当前输出而不使用硬编码?您能否演示如何通过尝试验证上述响应输出?如何像上面那样验证嵌套响应输出?你能详细说明一下你对分配赏金分数的想法吗?谢谢,你能提供一个示例json文档作为你的结果吗?然后我可以帮助您创建用于验证的模式