im trying to do some charts, using jquery jChartFX.
When Ive made the arrays in the same file using php it works great, how ever id like it to be grapped from another file, for that i try to use some json..
the array looks like this ( print_f($chartArray) )
Array (
[0] => Array (
[Procent rigtige] => 100
[Antal rigtige] => 4
[Antal mulige] => 4
[Date] => Januar-1970 )
[1] => Array (
[Procent rigtige] => 100
[Antal rigtige] => 4
[Antal mulige] => 4
[Date] => Februar-2014 )
)
How ever when i try an decode the json it looks like
Array (
[0] => stdClass Object (
[Procent rigtige] => 100
[Antal rigtige] => 4
[Antal mulige] => 4
[Date] => Januar-1970 )
[1] => stdClass Object (
[Procent rigtige] => 100
[Antal rigtige] => 4
[Antal mulige] => 4
[Date] => Februar-2014 )
)
Any way using jquery that i can parse the json from my php to the jquery and still keep it as arrays and not objects?
Or are there some way that i can do this smarter?
i use
echo json_encode($chartArray);
at my getUserStats.php and to get them im using >
$.ajax({
type: "GET",
url: "getUserStats.php", data: {'type': 'monthly'},
success: function(data) {
chart1.setDataSource(JSON.parse(data))
}
});
To parse a json in jquery, you can use jQuery.parseJSON()
In PHP, use json_encode() to encode array.
In your test.php, add header('Content-Type: application/json');
test.php
// header('Content-Type: application/json'); //change
$chartArray = array(
"0" => array(
"Procent rigtige" => 100,
"Antal rigtige" => 4,
"Antal mulige" => 4,
"Date" => "January-1970"
),
"1" => array (
"Procent rigtige" => 100,
"Antal rigtige" => 4,
"Antal mulige" => 4,
"Date" => "February-2014" )
);
echo json_encode($chartArray);
AJAX
$.ajax({
type: "GET",
url: "test.php",
data: {'type': 'monthly'},
// dataType: "json", // change
success: function(data) {
chart1.setDataSource(data); //change // output 100
}
});