Scott B
Scott B

Reputation: 1528

read json object that has two arrays in python using GAE

I created a json object in jquery and sent it via ajax as a post request. I logged both the arrays and it has the correct format.

$('#submittt').on('click', function () {
    var names = [];
    var times = [];

    $(".input-medium.name").each(function() {
        names.push($(this).val());
    });

   $(".input-medium.time").each(function() {
        times.push(parseInt($(this).val()));
   });

  console.log(JSON.stringify(names));
  console.log(JSON.stringify(times));

  var data = {"names":names, "times":times};
  console.log(data);
     $.ajax({
         url: "/step/" + vid,
         type: "POST",
         data: 'data=' + JSON.stringify(data),
         dataType: "json",
     });

Now I am trying to get the json object in python (I am using GAE) and access its 2 arrays. I believe I am supposed to do something like

json_raw = self.request.get('data')
jsonObj = json.loads(json_raw)
namelist = jsonObj[0]['names']
print namelist

But this isn't working, it says it can't decode the json object. Anyone know what I am doing wrong?

Upvotes: 1

Views: 163

Answers (1)

In your JavaScript, instead of

data: 'data=' + JSON.stringify(data),

do

data: JSON.stringify(data),

And in your Python remove the [0] when retrieving namelist.

Upvotes: 1

Related Questions