Please help me to understand what's wrong. I want to parse JSON reply as object.
PHP process.php code:
<?php
$return = array();
array_push($return['amount']="$amount");
array_push($return['fee']="$fee");
array_push($return['total']="$total");
echo json_encode($return);
?>
Returns JSON string:
{"amount":"350","fee":"0","total":"350"}
JS (jquery) code:
$.getJSON("process.php?amount="+amount, function(data,status) {
var obj = $.parseJSON(data);
alert (obj.amount);
});
I receive error:
SyntaxError: JSON Parse error: Unexpected identifier "object"
BUT! When I try to insert result instead data (but insert ' quotes left/right):
var obj = $.parseJSON('{"amount":"350","fee":"0","total":"350"}');
And I see alert = 350. So, it's working good.
I try to make something like that:
var jsonreply = "'"+data+"'";
var obj = $.parseJSON(jsonreply);
But received below error:
SyntaxError: JSON Parse error: Single quotes (') are not allowed in JSON
$.parseJSONon reponse, it has already been converted internally. To prove this tryconsole.log( $.type(data))in your success callback