0

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)) 
       }
});
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
Mathias
  • 197
  • 1
  • 4
  • 19

4 Answers4

2

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 json_decode to array

Community
  • 1
  • 1
Vlad V
  • 1,594
  • 1
  • 13
  • 28
  • Sounds right, how ever im parsing the data as ive updated my question into another file using jquery, any way that i can do the same there? – Mathias Feb 25 '14 at 13:59
  • I don't get what you mean by "parsing the data [...] into another file using jQuery". From the updated question, if your `test.php` file outputs a JSON, the `$.ajax(..., function(data) { - use data here, should be your JSON parsed into a javascript object - })`. If something doesn't work, you might want to use Chrome Dev Tools or Firebug to see if the request is sent properly and your `test.php` returns what you want. – Vlad V Feb 26 '14 at 10:31
1

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
  } 
});
Atanu Saha
  • 568
  • 4
  • 14
  • I have changed my answer. Now please check this out. – Atanu Saha Feb 25 '14 at 14:23
  • Please see my answer once again and modify your scripts. It will be better if you will edit your own question once again and add chart1.setDataSource() issue. – Atanu Saha Feb 27 '14 at 09:19
  • Just figured it out, i had to do JSON.parse(data) inside the ajax, pretty sure ive tried it before tho, but working now, and you helped getting there, thanks :) – Mathias Mar 07 '14 at 08:59
0

This might help :

$json = json_encode($yourArray);
Melchizedek
  • 1,057
  • 17
  • 29
FabriGad Ahmed
  • 156
  • 1
  • 8
  • I'm allready using that at the php, sorry just forgot to put it in the code, but its been edited now – Mathias Feb 25 '14 at 13:56
0

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.

ian
  • 234
  • 2
  • 9