将对象数组存储到会话Python

时间:2016-11-11 11:30:22

标签: python python-2.7 cherrypy

我想在POST请求中将对象数组存储到Session我收到以下错误:

预期字符串,dict发现

def POST(self):
    deviceArr1=[{"id":"01","name":"abc"},{"id":"02","name":"abcd"}]
    for device in deviceArr1:
        cherrypy.session['deviceArr'].append(device)
    return cherrypy.session['deviceArr']

另外,我想检查是否存在重复的对象。

1 个答案:

答案 0 :(得分:0)

您可以在for:

中尝试此代码
try:
    cherrypy.session['deviceArr'].append(device)
except:
    cherrypy.session['deviceArr'] = [device]