I'm trying to plot (with Flot) a pie chart with some data
var data = <?php echo json_encode($data)?>
The result I get from that is this:
var data = [ {"label":"Crear Usuario", "data":"2"}, {"label":"Impresoras", "data":"1"}, {"label":"Problema Correo", "data":"1"}, {"label":"Requisicion Equipo", "data":"1"}, {"label":"Sitio Web", "data":"1"} ]
The problem here is that I need the label
and data
without the quotes, I already tried json_encode($data, JSON_NUMERIC_CHECK);
but only removes the quotes from the numbers.
The following format is what I need:
var data = [ {label:"Crear Usuario",data:2}, ...
To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
Syntax. The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.
Using sizeof() function: This method check the size of array. If the size of array is zero then array is empty otherwise array is not empty.
In jQuery you can use the following code to encode the json: var data = {"key1":"value1", "key2":"value2","key3":"value3",...}; $. each(data,function(index,value)){ alert(index+" | "+value); });
First, you have to generate your array in php so the data's value are integers, not strings:
I emulated your array from your json_encode(), I guess it looks like this (or it should):
$array = array( array("label" => "Crear Usuario", "data" => 2), array("label" => "Impresoras", "data" => 1), array("label" => "Problema Correo", "data" => 1), array("label" => "Requisicion Equipo", "data" => 1), array("label" => "Sitio Web", "data" => 1) ); $data = json_encode($array);
Then you are missin in Javascript the JSON.parse() to actually make that output into a json object:
<script> var data = '<?php echo $data; ?>'; var json = JSON.parse(data); console.log(json); console.log(json[0]); </script>
The console.log()'s output this for me:
[Object, Object, Object, Object, Object] // First console.log(): one object with the 5 Objects. Object {label: "Crear Usuario", data: 2} // secons console log (json[0]) with the first object
Looks like what you need, am I right?
There's no difference between quoted and unquoted keys. The problem is with the quoting around the actual data values, since Flot requires numbers, not strings.
The json_encode function decides to whether to quote based on the type of data you're giving it. In this case it looks like whatever operations you're performing to create $data are producing string values instead of integers. You need to re-examine those operations, or explicitly tell PHP to interpret them as numbers, using (int) or (float) casting, or the intval/floatval functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With