如何从google云端点python返回动态json作为响应
我希望从google端点返回以下json如何从google云端点python返回动态json作为响应,python,google-app-engine,google-cloud-endpoints,Python,Google App Engine,Google Cloud Endpoints,我希望从google端点返回以下json {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]} data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}
{"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
下面是我如何声明我的端点
@endpoints.method(TokenAsInput,GetDepartmentListOutput,
path='getdepartmentlist', http_method='GET',
name='GetDepartmentList')
def getDepartmentList(self,request):
objResult = GetDepartmentListOutput()
objResult.data = dynamicJson
return objResult
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
但是我不知道如何声明GetDepartmentListOutput以便它可以映射上面的JSON。对象“arts”和“science”是动态的,可能存在也可能不存在 我使用ProtoRpc消息(位于云端点之下)并使用以下内容返回常规Json消息数据:
from protorpc import messages
class DetailMessage(messages.Message):
"""
General-format Json detail response
"""
data = GeneralField(1)
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
要设置结果,请执行以下操作:
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
GeneralField的定义如下:
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
class GeneralField(messages.Field):
"""
Allow for normal non-Message objects to be serialised to JSON.
This allows for variable result objects or dictionaries to be returned (Note: these objects must be Json serialisable).
"""
VARIANTS = frozenset([messages.Variant.MESSAGE])
DEFAULT_VARIANT = messages.Variant.MESSAGE
def __init__(self,
number,
required=False,
repeated=False,
variant=None):
"""Constructor.
Args:
number: Number of field. Must be unique per message class.
required: Whether or not field is required. Mutually exclusive to
'repeated'.
repeated: Whether or not field is repeated. Mutually exclusive to
'required'.
variant: Wire-format variant hint.
Raises:
FieldDefinitionError when invalid message_type is provided.
"""
super(GeneralField, self).__init__(number,
required=required,
repeated=repeated,
variant=variant)
def __set__(self, message_instance, value):
"""Set value on message.
Args:
message_instance: Message instance to set value on.
value: Value to set on message.
"""
if isinstance(value, list):
if len(value) > 0:
self.type = type(value[0])
else:
self.type = type(self)
else:
self.type = type(value)
self.__initialized = True
super(GeneralField, self).__set__(message_instance, value)
def __setattr__(self, name, value):
"""Setter overidden to allow assignment to fields after creation.
Args:
name: Name of attribute to set.
value: Value to assign.
"""
object.__setattr__(self, name, value)
def value_from_message(self, message):
"""Convert a message to a value instance.
Used by deserializers to convert from underlying messages to
value of expected user type.
Args:
message: A message instance of type self.message_type.
Returns:
Value of self.message_type.
"""
return message
def value_to_message(self, value):
"""Convert a value instance to a message.
Used by serializers to convert Python user types to underlying
messages for transmission.
Args:
value: A value of type self.type.
Returns:
An instance of type self.message_type.
"""
return value
注:GeneralField源自其他ProtoRpc消息代码,并覆盖字段的set和setattr方法,以允许在ProtoRpc消息中使用普通(json可序列化)对象或字典。您可能需要调整此方法以适应您的目的
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
注2:我不确定云端点会是什么样子,但这可能值得一试。我使用ProtoRpc消息(位于云端点之下)并使用以下内容返回常规Json消息数据:
from protorpc import messages
class DetailMessage(messages.Message):
"""
General-format Json detail response
"""
data = GeneralField(1)
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
要设置结果,请执行以下操作:
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
GeneralField的定义如下:
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
class GeneralField(messages.Field):
"""
Allow for normal non-Message objects to be serialised to JSON.
This allows for variable result objects or dictionaries to be returned (Note: these objects must be Json serialisable).
"""
VARIANTS = frozenset([messages.Variant.MESSAGE])
DEFAULT_VARIANT = messages.Variant.MESSAGE
def __init__(self,
number,
required=False,
repeated=False,
variant=None):
"""Constructor.
Args:
number: Number of field. Must be unique per message class.
required: Whether or not field is required. Mutually exclusive to
'repeated'.
repeated: Whether or not field is repeated. Mutually exclusive to
'required'.
variant: Wire-format variant hint.
Raises:
FieldDefinitionError when invalid message_type is provided.
"""
super(GeneralField, self).__init__(number,
required=required,
repeated=repeated,
variant=variant)
def __set__(self, message_instance, value):
"""Set value on message.
Args:
message_instance: Message instance to set value on.
value: Value to set on message.
"""
if isinstance(value, list):
if len(value) > 0:
self.type = type(value[0])
else:
self.type = type(self)
else:
self.type = type(value)
self.__initialized = True
super(GeneralField, self).__set__(message_instance, value)
def __setattr__(self, name, value):
"""Setter overidden to allow assignment to fields after creation.
Args:
name: Name of attribute to set.
value: Value to assign.
"""
object.__setattr__(self, name, value)
def value_from_message(self, message):
"""Convert a message to a value instance.
Used by deserializers to convert from underlying messages to
value of expected user type.
Args:
message: A message instance of type self.message_type.
Returns:
Value of self.message_type.
"""
return message
def value_to_message(self, value):
"""Convert a value instance to a message.
Used by serializers to convert Python user types to underlying
messages for transmission.
Args:
value: A value of type self.type.
Returns:
An instance of type self.message_type.
"""
return value
注:GeneralField源自其他ProtoRpc消息代码,并覆盖字段的set和setattr方法,以允许在ProtoRpc消息中使用普通(json可序列化)对象或字典。您可能需要调整此方法以适应您的目的
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)
注2:我不确定云端点会是什么样子,但这可能值得一试。可能的重复
data = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}
return DetailMessage(data=data)