2
我有一个使用JSON.stringify转换为字符串的数组,以便我可以通过ajax将数据发送给Python处理程序。这使我可以将数据保存为括号,引号和全部。我需要在某种可用的列表或数组中使用这些数据......而不仅仅是一个字符串。我很感激任何煽动你可以给,谢谢。将JSON字符串(通过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();
Python的模型:
class AvailableTimes(db.Model):
user = db.StringProperty()
timezone = db.StringProperty()
ids = db.StringProperty()
哇,这是一个快速的响应,alecxe!我对Python很新,可以帮助我理解“u”是什么意思?并会影响我如何使用字符串? - 感谢 –
'u'只是一个前缀,表示该字符串是'unicode'。请参阅http://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string。 – alecxe
谢谢alecxe,那很棒! –