4

我有一个使用 JSON.stringify 转换为字符串的数组,以便我可以通过 ajax 将数据发送到 Python 处理程序。这让我可以将数据保存为一个字符串……括号、引号等等。我需要某种可用列表或数组中的这些数据......而不仅仅是一个字符串。我很感激你能给予的任何煽动,谢谢。

保存在数据库中的字符串格式(数据存储 - 使用 GAE):

["SU15AM","SU3AM","SU4AM","SU45AM","SU4PM","M3AM"]

javascript/ajax 代码:

var ids = new Array();

$('.ui-selected').each(function(){ 
    ids.push($(this).attr('value'));    //adds items selected to the array so that they can be passed via ajax
    console.log('**item added to data object**');
});

string_ids = JSON.stringify(ids, null);   //converts array object to a string to pass via ajax

$.ajax({                
    type: "POST",
    url: '/schedule',
    data: {'ids': string_ids},      //string of selected items 
});

Python处理程序:

class ScheduleHandler(BaseHandler2):
    time_ids = self.request.get('ids')
    times = AvailableTimes(ids = time_ids)         
    times.put();

蟒蛇模型:

class AvailableTimes(db.Model): 
    user = db.StringProperty()    
    timezone = db.StringProperty()
    ids = db.StringProperty()
4

1 回答 1

4

通过json.loads()加载它:

>>> import json
>>> s = '["SU15AM","SU3AM","SU4AM","SU45AM","SU4PM","M3AM"]'
>>> l = json.loads(s)
>>> l
[u'SU15AM', u'SU3AM', u'SU4AM', u'SU45AM', u'SU4PM', u'M3AM']
>>> type(l)
<type 'list'>  
于 2013-08-28T19:38:41.793 回答