Return JSON 数组作为在 Python 中使用 Google Cloud Endpoints Framework 的响应
Return JSON array as response using Google Cloud Endpoints Framework in Python
我正在将 Google Endpoints Framework 与 Python (https://cloud.google.com/endpoints/docs/frameworks/python/get-started-frameworks-python) 一起使用,并且一直在使用它构建 REST API。
我能够 return JSON 在响应中对象(字典),例如:
{
"items":[
{
"id": "brand_1_id",
"name": "Brand 1"
},
{
"id": "brand_2_id",
"name": "Brand 2"
}
]
}
但是,我无法将 return 一个 JSON 数组(列表)作为响应,例如:
[
{
"id": "brand_1_id",
"name": "Brand 1"
},
{
"id": "brand_2_id",
"name": "Brand 2"
}
]
以下是我用来return响应
的代码片段
以下是为发送响应而创建的 类:
class BrandResponse(messages.Message):
id=messages.StringField(1, required=True)
brandName = messages.StringField(2, required=True)
brandEmail=messages.StringField(3, required=True)
brandPhone=messages.StringField(4,required=True)
brandAddress=messages.StringField(5,required=True)
brandCity=messages.StringField(6,required=True)
brandState=messages.StringField(7,required=True)
brandCountry=messages.StringField(8,required=True)
brandPostalCode=messages.StringField(9,required=True)
class MultipleBrandResponse(messages.Message):
items=messages.MessageField(BrandResponse,1,repeated=True)
处理请求的方法如下:
@endpoints.method(
MULTIPLE_BRAND_PAGINATED_CONTAINER,
MultipleBrandResponse,
path='brand',
http_method='GET',
name='Get Brands',
audiences=firebaseAudience
)
def getBrands(self,request):
brandResponse=[]
query = BrandDB.query()
brands = query.fetch(request.limit, offset=request.start)
for brand in brands:
brandResponse.append(BrandResponse(
id=brand.key.urlsafe(),
brandName=brand.name,
brandAddress=brand.address,
brandCity=brand.city,
brandState=brand.state,
brandCountry=brand.country,
brandPostalCode=brand.postalCode,
brandPhone=brand.phone,
brandEmail=brand.email))
print("Brand count: "+str(brandResponse.count))
if len(brandResponse) == 0:
raise endpoints.NotFoundException("No brands")
return MultipleBrandResponse(items=brandResponse)
知道如何直接 return JSON 数组而不是将键包装在 JSON 对象中。
该框架是围绕返回 Protocol Buffer 消息而不是 JSON 设计的。您可能已经指定了该字典结构匹配的消息;没有看到你的代码很难说。
class Car(messages.Message):
brand = messages.StringFeild(1)
color = messages.StringFeild(2)
class ResponseCars(messages.Message):
cars = messages.MessageField(Car,1, repeated = True)
@endpoints.api(name='mycar', version='v1')
class MyCar(remote.Service):
@endpoints.method(
message_types.VoidMessage,
ResponseCars,
path='getCars',
http_method='GET',
name='get_cars')
def get_cars(self, request):
cars = [Car(brand='bmw', color = 'black'), Car(brand='toyota', color='white')]
return ResponseCars(cars = cars)
#ref : https://cloud.google.com/appengine/docs/standard/python/tools/protorpc/messages/messagefieldclass
我正在将 Google Endpoints Framework 与 Python (https://cloud.google.com/endpoints/docs/frameworks/python/get-started-frameworks-python) 一起使用,并且一直在使用它构建 REST API。
我能够 return JSON 在响应中对象(字典),例如:
{
"items":[
{
"id": "brand_1_id",
"name": "Brand 1"
},
{
"id": "brand_2_id",
"name": "Brand 2"
}
]
}
但是,我无法将 return 一个 JSON 数组(列表)作为响应,例如:
[ { "id": "brand_1_id", "name": "Brand 1" }, { "id": "brand_2_id", "name": "Brand 2" } ]
以下是我用来return响应
的代码片段以下是为发送响应而创建的 类:
class BrandResponse(messages.Message):
id=messages.StringField(1, required=True)
brandName = messages.StringField(2, required=True)
brandEmail=messages.StringField(3, required=True)
brandPhone=messages.StringField(4,required=True)
brandAddress=messages.StringField(5,required=True)
brandCity=messages.StringField(6,required=True)
brandState=messages.StringField(7,required=True)
brandCountry=messages.StringField(8,required=True)
brandPostalCode=messages.StringField(9,required=True)
class MultipleBrandResponse(messages.Message):
items=messages.MessageField(BrandResponse,1,repeated=True)
处理请求的方法如下:
@endpoints.method(
MULTIPLE_BRAND_PAGINATED_CONTAINER,
MultipleBrandResponse,
path='brand',
http_method='GET',
name='Get Brands',
audiences=firebaseAudience
)
def getBrands(self,request):
brandResponse=[]
query = BrandDB.query()
brands = query.fetch(request.limit, offset=request.start)
for brand in brands:
brandResponse.append(BrandResponse(
id=brand.key.urlsafe(),
brandName=brand.name,
brandAddress=brand.address,
brandCity=brand.city,
brandState=brand.state,
brandCountry=brand.country,
brandPostalCode=brand.postalCode,
brandPhone=brand.phone,
brandEmail=brand.email))
print("Brand count: "+str(brandResponse.count))
if len(brandResponse) == 0:
raise endpoints.NotFoundException("No brands")
return MultipleBrandResponse(items=brandResponse)
知道如何直接 return JSON 数组而不是将键包装在 JSON 对象中。
该框架是围绕返回 Protocol Buffer 消息而不是 JSON 设计的。您可能已经指定了该字典结构匹配的消息;没有看到你的代码很难说。
class Car(messages.Message):
brand = messages.StringFeild(1)
color = messages.StringFeild(2)
class ResponseCars(messages.Message):
cars = messages.MessageField(Car,1, repeated = True)
@endpoints.api(name='mycar', version='v1')
class MyCar(remote.Service):
@endpoints.method(
message_types.VoidMessage,
ResponseCars,
path='getCars',
http_method='GET',
name='get_cars')
def get_cars(self, request):
cars = [Car(brand='bmw', color = 'black'), Car(brand='toyota', color='white')]
return ResponseCars(cars = cars)
#ref : https://cloud.google.com/appengine/docs/standard/python/tools/protorpc/messages/messagefieldclass