【问题标题】:object not is not JSON serializable对象 not 不是 JSON 可序列化的
【发布时间】:2013-11-02 16:27:27
【问题描述】:

我正在尝试将数据库中的数据加载到 html 页面中。基本上,当用户访问他们的个人资料并单击“购买历史”时,它应该查询数据库并显示用户购买的所有产品。

我正在尝试使用 ajax 和 json 执行此操作,但出现错误:

TypeError: <gluon.dal.Field object at 0x091CCD90> is not JSON serializable

下面是代码:

def prodHistoryJson():
    productName = db.sale.title
    productCost = db.sale.price
    prodShipAdd = db.sale.shipping_address
    prodShipCity = db.sale.shipping_city
    prodShipState = db.sale.shipping_state
    prodShipZipCode = db.sale.shipping_zip_code

    myproducts = {'prodName':productName, 
                  'cost':productCost, 
                  'shipAdd':prodShipAdd, 
                  'shipCity':prodShipCity,
                  'shipState':prodShipState, 
                  'shipZipCode':prodShipZipCode}

    import  gluon.contrib.simplejson as json
    returnData = json.dumps(myproducts)

return returnData

下面是jquery:

$.ajax( {
        url:'/suzannecollins/onlineStore/prodHistoryJson',
        data: { message: "Your products purchase history is listed below" },
        success: function(msg) {

           try {
                myproducts=JSON.parse(msg);
            }
            catch(err) {
                console.log(" error");
            }
            // place returned value in the DOM
            $('#returnData').html(myproducts.title + myproducts.price + myproducts.shipping_address
             + myproduct.shipping_state + myproducts.shipping_city + myproducts.shipping_zip_code);
    }
});

我做错了什么? 如果我只是以更简单的方式执行此操作,即用户点击 purchase_History 按钮并查询数据库并显示购买的产品,我可以让这一切正常工作。我如何用上面的代码做同样的事情?

【问题讨论】:

  • 这与jQuery无关。 gluon.dal.Field 无法序列化为 JSON。
  • 似乎db.sale.title 等不是您希望的字符串,而是字段描述符。你是怎么得到这些的?
  • 旁注:删除 AJAX 代码中的 try-catch。最好看到由于页面加载不正确而导致的错误,而不是默默地将它们扫到您永远看不到它们的控制台。由于它现在是结构化的,您的代码可能会抛出异常但仍然尝试修改页面,这没有意义。
  • 同意@musical_coder,对评论的移动回答:如果看到更多的堆栈跟踪和你的数据库字段的声明会很有趣。一些 db 字段不能直接序列化(如 db.GeoPt、日期、db.Key 等),您需要“手动”进行。我有同样的问题并通过使用这个答案解决了它。
  • @Amadan,它们在销售表中定义。 music_coder,现在我只是尝试使用 json 加载原始数据,但是我已按要求从代码中删除了 try catch。

标签: javascript jquery python json


【解决方案1】:

所以昨晚感谢@Amadan,我们终于弄清楚了如何使用json查询数据库并序列化python对象以显示结果...

def pruchaseHistoryJson():
    if auth.user:
        rows = db(db.sale.auth_id == auth.user.id
                 ).select(db.sale.title, 
                          db.sale.price,
                          db.sale.shipping_address,
                          db.sale.shipping_state,
                          db.sale.shipping_city,
                          db.sale.shipping_zip_code)
    else:
        redirect(URL('default', 'user/login'))

import  gluon.contrib.simplejson as json
prodHistory = json.dumps([{'name': i.title, 
                           'prodValue':i.price,
                           'shipAdd':i.shipping_address,
                           'shipCity':i.shipping_city,
                           'shipState':i.shipping_state,
                           'shipCode':i.shipping_zip_code} for i in rows])
return prodHistory

这段代码运行良好并按预期显示结果,现在回到为此编写 jquery 函数。

【讨论】:

    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 2013-05-23
    • 2015-02-24
    • 2014-10-23
    • 1970-01-01
    • 2018-09-12
    • 2018-08-16
    • 2016-03-03
    相关资源
    最近更新 更多