我如何对这个视图进行 json 编码? [英] how do I json encode this view?

查看:55
本文介绍了我如何对这个视图进行 json 编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 和 Pyramid 的新手.

我有一个可以进行分页的视图.我如何对它进行 json 编码?当我尝试这个时,我收到一个错误对象在 0x2d16d90> 不是 JSON 可序列化的":

@view_config(route_name="paginate")定义分页(请求):查询 = DBSession.query(MyTable)page_url = paginate.PageURL_WebOb(请求)客户 = paginate.Page(查询,page=int(request.params.get("page", 1)),items_per_page=25,url=page_url)如果 request.params 中的部分":# 渲染部分列表页面return render_to_response("templates/my_json.jinja2",{客户":json.dumps(客户)},请求=请求)别的:# 渲染完整的列表页面return render_to_response("templates/my.jinja2",{客户":客户},请求=请求)

解决方案

JSON 是一种严格的文本格式.您不能只是将 SQLAlchemy 扔给编码器并期望它能够正常工作,您需要决定对象的编码是什么样的.

一种方法是让您的 SQLAlchemy 对象实现一个额外的方法,该方法返回一个简单的 Python 字典;一个可以被序列化的.这意味着它只能使用字符串作为键,并且只能包含列表、元组、数字和/或字符串.

示例方法可以是:

def json_dump(self):return dict(name=self.name, phone=[p.number for p in self.phonenumbers])

这将返回一个带有 namephone 键的字典,其中 phone 键包含来自(纯编例如目的)电话号码关系.

然后,您可以使用该信息创建一个 json.dumps 可以处理的新字典列表:

如果 request.params 中的部分":customer_json_data = [c.json_dump() for c in customers]# 渲染部分列表页面return render_to_response("templates/my_json.jinja2",{客户":json.dumps(customer_json_data)},请求=请求)

即将推出的 Pyramid 1.4 添加了对通过 __json__ 方法钩子,以及支持适配器为您进行转换.>

New to python and Pyramid.

I have a view that does pagination. How do I json encode it? I get an error "object at 0x2d16d90> is not JSON serializable" when I try this:

@view_config(route_name="paginate")  
def paginate(request):
    query = DBSession.query(MyTable)
    page_url = paginate.PageURL_WebOb(request)
    customers = paginate.Page(query, 
                     page=int(request.params.get("page", 1)), 
                     items_per_page=25, 
                     url=page_url)

    if "partial" in request.params:
        # Render the partial list page
        return render_to_response("templates/my_json.jinja2",
                                  {"customers": json.dumps(customers)},
                                  request=request)
    else:
        # Render the full list page
        return render_to_response("templates/my.jinja2",
                                  {"customers": customers},
                                  request=request)

解决方案

JSON is a strict textual format. You cannot just throw SQLAlchemy at the encoder and expect this to Just Work, you need to decide what the encoding for your objects will look like.

One way is for your SQLAlchemy objects to implement an extra method that returns a simple python dictionary; one that can be serialized. That means it can only use strings for keys, and can only contain lists, tuples, numbers and / or strings.

An example method could be:

def json_dump(self):
    return dict(name=self.name, phone=[p.number for p in self.phonenumbers])

This returns a dictionary with name and phone keys, where the phone key contains a list of phone numbers from a (purely made up for example purposes) phonenumbers relationship.

You can then use that information to create a new list of dictionaries that json.dumps can handle:

if "partial" in request.params:
    customer_json_data = [c.json_dump() for c in customers]
    # Render the partial list page
    return render_to_response("templates/my_json.jinja2",
                              {"customers": json.dumps(customer_json_data)},
                              request=request)

The upcoming Pyramid 1.4 adds specific support for serializing custom objects in the json renderer via the __json__ method hook, as well as support for adapters to do the conversion for you.

这篇关于我如何对这个视图进行 json 编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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