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,那很棒! –