Here is the server side code I am trying to send json
$x = array();
$timestamp = strtotime('22-09-2008');
$x["x"] = $timestamp;
$x["y"] = 22;
$val = '[{ "name": "weight", "dataPoints": ['.json_encode($x).'] }]';
echo json_encode($val);
So output for above code looks like
"[{ \"name\": \"weight\", \"dataPoints\": [{\"x\":1222041600,\"y\":22}] }]"
Below is the client side code I get the data via Jquery getJSON
var jqxhr = $.getJSON( "https://domain/gettracker.php?id="+id, function(data) {
console.log(data);
})
I suppose getJson converts json to object automatically , but it logs the raw json like below
"[ { name: "weight", dataPoints: [{"x":1222041600,"y":22}] } ]"
I tried to do json parse , but i get error.
I guess I am not sending the data properly via php.Can some one guide me ?
$valis a string.json_encode($val)encodes that string as JSON, so you get"....."which is valid JSON. jQuery is callingJSON.parse, which returns the....part. It works perfectly fine, just not like you intended because you are not preparing your data correctly. I recommend to read the documentation and examples aboutjson_decode: php.net/manual/en/function.json-encode.phpjson_encodeconverts the value to JSON (and returns a string). If you are already having a string containing JSON, you don't want to calljson_encodeon it. Makes sense?