Python 在RESTAPI中返回JSON对象的长数组

Python 在RESTAPI中返回JSON对象的长数组,python,json,rest,streaming,Python,Json,Rest,Streaming,假设我有一个很大(但有限)的JSON对象数组,我想从RESTAPI返回。执行此操作的标准方法是返回整个阵列,这意味着任何客户端都必须等到整个阵列下载完成后才能对其进行解释 如何以允许一次处理一个对象的方式返回所述数组 如果我使用Python中的bottle和urllib2库,我会想象这样的接口 server.py @bottle.get("/long/array") # reachable from http://localhost/long/array @streaming_json

假设我有一个很大(但有限)的JSON对象数组,我想从RESTAPI返回。执行此操作的标准方法是返回整个阵列,这意味着任何客户端都必须等到整个阵列下载完成后才能对其进行解释

如何以允许一次处理一个对象的方式返回所述数组

如果我使用Python中的
bottle
urllib2
库,我会想象这样的接口

server.py

@bottle.get("/long/array")  # reachable from http://localhost/long/array
@streaming_json             # indicates that this function returns a generator of JSON-serializable objects
def long_array():
  for obj in really_long_array:
     yield obj
for line in urllib2.urlopen("http://localhost/long/array"):
   print json.loads(obj)
client.py

@bottle.get("/long/array")  # reachable from http://localhost/long/array
@streaming_json             # indicates that this function returns a generator of JSON-serializable objects
def long_array():
  for obj in really_long_array:
     yield obj
for line in urllib2.urlopen("http://localhost/long/array"):
   print json.loads(obj)

是否存在这样的接口?我该如何实现一个呢?

您的rest应用程序可以在元数据中返回一个页码,因此用户可以在请求中指定要获取的页面,这样JSON就不长了,出于明显的原因,不建议返回长响应。在@PepperoniPizza有一个好主意的众多原因中,如果客户端由于网络错误丢失了一个请求,它可以在丢失的页面上继续,而不是重新开始。