Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: JSON Parse error: Unexpected identifier "object"

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

like image 475
Vasvas Avatar asked Oct 28 '13 01:10

Vasvas


People also ask

What does JSON parse error mean?

parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.

Does JSON parse throw an error?

What went wrong? JSON. parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.

What is JSON parse error unexpected EOF?

Unexpected EOF (end of file) means that there might be something like a missing or duplicate '}' or ',' character. Check in the tone studio files for any . json file extensions and try to open them in a code editor like vscode for example.

What error does JSON parse () throw when the string to parse is not valid JSON?

Exceptions. Throws a SyntaxError exception if the string to parse is not valid JSON.


2 Answers

getJSON parses the JSON for you — calling $.parseJSON will convert the object into the string [object Object] and then try to parse that, giving you an error. Just omit the $.parseJSON call and use data directly.


Furthermore, I should note that the calls to array_push are strange and unnecessary. array_push usually takes an array and a value to push on to it, but (for example) in your first line you're setting $return['amount'] to "$amount" and then passing $return['amount'] to array_push, which does nothing at best and might give you a warning or error at worst. You'd get the exact same behavior if you did this:

$return['amount']="$amount";
$return['fee']="$fee";
$return['total']="$total";

Then you might also realize that the quotes around, say, "$amount" are unnecessary, and you could actually do this:

$return['amount']=$amount;
$return['fee']=$fee;
$return['total']=$total;

Finally, you can actually condense all five lines using some special array syntax very easily:

echo json_encode(array(
    'amount' => $amount,
    'fee' => $fee,
    'total' => $total
));

This is quite a bit nicer if I do say so myself.

like image 74
icktoofay Avatar answered Sep 21 '22 12:09

icktoofay


Actually u dont need to parse it. U can directly access it

$.getJSON("process.php?amount="+amount, function(data,status) {
 alert (data.amount); 
});
like image 38
Vicky Gonsalves Avatar answered Sep 22 '22 12:09

Vicky Gonsalves