从web2py以JSON格式返回列表 [英] return a list as JSON from web2py

查看:198
本文介绍了从web2py以JSON格式返回列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过web2py将列表作为JSON列表返回?

Is there any way to return a list as a JSON list from web2py?

当我打到以.json结尾的路线时,web2py会将字典转换为有效的JSON.但是,如果返回值是列表,则其行为异常

when I hit my route ending in .json, web2py converts dictionaries into valid JSON. If the return value is a list however, it behaves unexpectedly

return ["test", "test"]

显示

testtest

return [dict(test="test"), dict(test="test")]

完全破坏了它(chrome抱怨ERR_INVALID_CHUNKED_ENCODING).显然,预期行为分别是以下有效的JSON字符串:

breaks it completely (chrome complains with ERR_INVALID_CHUNKED_ENCODING). The expect behaviour is obviously the following valid JSON strings respectively:

["test", "test"]

[{"test":"test"}, {"test":"test"}]

推荐答案

@service.json装饰器中注册json服务,并使用`app/controller/call/json/method'调用它,这给了我预期的行为:

registering a json service with the @service.json decorator and calling it with `app/controller/call/json/method' gave me the behaviour I expected:

def f():
    return ["test1", "test2"]

def call():
    return service()

响应正文是

["test1", "test2"]

内容类型设置为json

with content-type set to json

这篇关于从web2py以JSON格式返回列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆