json from php to jquery

5.6k Views Asked by At

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)) 
       }
});
4

There are 4 best solutions below

3
On BEST ANSWER

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
  } 
});
2
On

This is not related to jQuery or anything else that you're doing, it looks like an incorrect use of json_decode.

You probably decode the JSON using $arr = json_decode($json_string) but what you want is $arr = json_decode($json_string, true).

The second argument to json_decode, assoc, controls whether the return should be an associative array or the objects you are getting in the representation. See json_decode documentation.

This is a possible duplicate of Use json_decode() to create array insead of an object

1
On

This might help :

$json = json_encode($yourArray);
0
On

If you have a file named make_json.php in your root, you could do this with jQuery:

var json = $.getJSON('/make_json.php');

Then in your make_json.php file you would just have to echo out some json. Use json_encode:

// ... assuming your array is called $array
echo json_encode($array);

You don't need jQuery do do this, but it certainly makes it simpler.