javascript 语法错误:JSON 解析错误:意外的标识符“对象”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19625960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: JSON Parse error: Unexpected identifier "object"
提问by Vasvas
Please help me to understand what's wrong. I want to parse JSON reply as object.
请帮助我了解出了什么问题。我想将 JSON 回复解析为对象。
PHP process.php code:
PHP process.php 代码:
<?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:
返回 JSON 字符串:
{"amount":"350","fee":"0","total":"350"}
JS (jquery) code:
JS(jquery)代码:
$.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"
语法错误:JSON 解析错误:意外的标识符“对象”
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.
我看到 alert = 350。所以,它运行良好。
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
语法错误:JSON 解析错误:JSON 中不允许使用单引号 (')
回答by icktoofay
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.
getJSON
为您解析 JSON —?calling$.parseJSON
会将对象转换为字符串[object Object]
,然后尝试解析它,给您一个错误。直接省略$.parseJSON
调用data
即可。
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:
此外,我应该注意到对 的调用array_push
很奇怪且不必要。array_push
通常需要一个数组和一个值来推送它,但是(例如)在你的第一行中你设置$return['amount']
为"$amount"
然后传递$return['amount']
给array_push
,这充其量什么都不做,最坏的情况可能会给你一个警告或错误。如果你这样做,你会得到完全相同的行为:
$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:
然后你可能也会意识到周围的引号"$amount"
是不必要的,你实际上可以这样做:
$return['amount']=$amount;
$return['fee']=$fee;
$return['total']=$total;
Finally, you can actually condense all five lines using some special array
syntax very easily:
最后,您实际上可以使用一些特殊array
语法非常轻松地压缩所有五行:
echo json_encode(array(
'amount' => $amount,
'fee' => $fee,
'total' => $total
));
This is quite a bit nicer if I do say so myself.
如果我自己这么说,那就更好了。
回答by Vicky Gonsalves
Actually u dont need to parse it. U can directly access it
其实你不需要解析它。你可以直接访问它
$.getJSON("process.php?amount="+amount, function(data,status) {
alert (data.amount);
});
回答by lolrs2013
It seems like your error is here:
您的错误似乎在这里:
var jsonreply = "'"+data+"'";
Try to escape those ' with "\". Like
尝试用“\”转义那些 '。喜欢
var jsonreply = "\'"+data+"\'";