0

From a datapicker i send 2 dates to a php. I'm trying to print a group of value from json to use in a statistic.

$.ajax({ 
      url: 'calctimestat.php', 
      method: 'POST',
      data: {dateone:dateOne,datetwo:dateTwo},
      success: function(data) 
      { 
          var obj = jQuery.parseJSON(data);
          console.log(JSON.stringify(obj));
       }
});

Ajax callback returns into log:

[{"dt":"2014-06-04","qt":"0"},{"dt":"2014-06-05","qt":"0"},{"dt":"2014-06-06","qt":"0"},{"dt":"2014-06-07","qt":"0"},{"dt":"2014-06-08","qt":"0"}]

I tried with:

 var date = "dt"
 console.log(JSON.stringify(obj.date));
 var quantity = "qt"
 console.log(JSON.stringify(obj.quantity));

But always returns undefined. I need to have something like this:

[0,0,0,0...]

and

[2014-06-04,2014-06-05,2014-06-06,2014-06-07...]

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 3
    Your `obj` object doesn't have a `date` property. I assume you wanted `obj.dt` or `obj[date]`. Also, it looks like `obj` is actually an *array* (of objects). – gen_Eric Jun 18 '14 at 17:25
  • 1
    check this out - looks like what you want http://stackoverflow.com/questions/14528385/how-to-convert-json-object-to-javascript-array – ewizard Jun 18 '14 at 17:32

1 Answers1

0

the author of the question clearly does not need an answer, but maybe the direction of its solution will help others.with this return, to get two arrays, as described in the requirement, it is enough to iterate through the values of objects in the array and get their values by key, decomposing them into the corresponding arrays.

/* IF Ajax callback data = [{"dt":"2014-06-04","qt":"0"},{"dt":"2014-06-05","qt":"0"},{"dt":"2014-06-06","qt":"0"},{"dt":"2014-06-07","qt":"0"},{"dt":"2014-06-08","qt":"0"}]
    */
    $.ajax({ 
      url: 'calctimestat.php', 
      method: 'POST',
      data: {dateone:dateOne,datetwo:dateTwo},
      success: function(data) 
      { 
          var obj = jQuery.parseJSON(data);
          var result = [];
          var result1 = [];
          for(var i in obj)
          {
              result.push(obj[i]['qt']);
              result1.push(obj[i]['dt']);
              }
          console.log(JSON.stringify(result));
          console.log(JSON.stringify(result1));
       }
});
romanown
  • 325
  • 2
  • 9