4

I have an array that was converted to a string using JSON.stringify so that I could send the data by ajax to a Python handler. This allowed me to save the data as a sting...brackets, quotes, and all. I need this data in some kind of usable list or array...not just a string. I appreciate any incite you can give, thanks.

The format of the string that is being saved in the database (datastore - using GAE):

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

The javascript/ajax code:

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 Handler:

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

Python model:

class AvailableTimes(db.Model): 
    user = db.StringProperty()    
    timezone = db.StringProperty()
    ids = db.StringProperty()
Matt Ellis
  • 143
  • 2
  • 2
  • 16

1 Answers1

4

Load it via 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'>  
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Wow that was a fast response, alecxe! I am pretty new to Python, can help me understand what the "u" means? and will that affect how i can use the string? -Thanks – Matt Ellis Aug 28 '13 at 19:47
  • `u` is just a prefix that means that the string is `unicode`. See http://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string. – alecxe Aug 28 '13 at 19:48