为什么json_encode后JSON.parse不起作用? [英] Why doesn't JSON.parse after json_encode doesn't work?

查看:394
本文介绍了为什么json_encode后JSON.parse不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这对我不起作用?

Why won't this work for me?

<script>
JSON.parse(<?php echo json_encode(array('test123', 'test456')); ?>);
</script>

我收到此错误:

SyntaxError:JSON.parse:JSON数据后出现意外的非空白字符

推荐答案

PHP的 json_encode 生成一个字符串,但它是内存中的实际字符串,而不是PHP语法中的字符串,所以当 echo 编辑。当回显到JavaScript代码的上下文中时,它会打印出完全有效的JSON,但JS解释器不会将其视为字符串,因为它不包含在引号中。没有它们,JS会看到一个用文字语法表示的数组(因为JSON从JS文字语法的子集中借用了自己的语法)。因此,尝试解析它会引发错误。

PHP's json_encode produces a string, but it's an actual string in memory, not one in PHP syntax, so it's not wrapped in quotes when echoed. When echoed into the context of JavaScript code, it prints perfectly valid JSON, but the JS interpreter would not see it as a string because it's not wrapped in quotes. Without them, JS sees an array denoted in literal syntax (because JSON borrows its own syntax from a subset of JS literal syntax). Thus, trying to parse it would throw an error.

如果你的代码运行了,这就是输出源中实际显示的内容:

If your code ran, this is what would actually show up in a the outputted source:

<script>
var obj = ["test123", "test456"];
</script>

As obj 现在持有一个数组(其中用JS文字语法表示,通过 JSON.parse 运行它会导致错误。

As obj now holds an array (which was denoted in JS literal syntax), running it through JSON.parse would cause an error.

想一想就像你在PHP中用字符串写的一句话,然后回应:

Think of it like a sentence you wrote in a string in PHP, then echoed:

<?php
$sentence = 'I like beer.':
echo $sentence;
?>

这会产生输出:

I like beer.

请注意没有包装报价。现在,想象一下手工编写一大块JSON并回复它:

Notice no wrapping quotes. Now, imagine writing a chunk of JSON "by hand" and echoing it:

<?php
$json = '{"foo": "bar"}':
echo $json;
?>

这将输出:

{"foo": "bar"}

再次注意不包装引号。对变量 $ json 的赋值在PHP中生成与调用 json_encode 相同的字符串数据。所以回显你的调用输出也没有引号。

Again, notice no wrapping quotes. That assignment to the variable $json produces the same string data in PHP as your call to json_encode. So echoing the output of your call also results in no quotes.

现在,看看最后一个输出块 - 如果遇到类似的事情,JavaScript引擎会做什么那个?

Now, look at that last output chunk--what would a JavaScript engine do if it ran across something like that?

所以,要用 JSON.parse 进行解析,你需要JS将其解释为一个字符串。 JS引擎的解析。如果您将PHP包装在单引号中:

So, to parse with JSON.parse, you need something JS will interpret as a string during the JS engine's parse. If you wrapped your PHP in single quotes as such:

<script>
var obj = '<?php echo json_encode(array('test123', 'test456')); ?>';
</script>

然后输出为:

<script>
var obj = '["test123", "test456"]';
</script>

由于这是一个格式正确的字符串,现在是JSON,可以传递给 JSON.parse

As this is a string in proper format, it is now JSON and could be passed to JSON.parse.

注意:简单包装单引号并不安全,不过,因为PHP编码的结构可能包含一个包含单引号的字符串。这会导致JavaScript中出现语法错误。)

(Note: Simply wrapping in single quotes isn't safe, though, as the structure which PHP encoded may have a string within which contains a single quote. That would result in a syntax error in your JavaScript.)

如前所述,无论如何这都不是必需的 - 你可以把你的非引用的JSON交给JS并跳过解析,因为JS解释器会按原样解析它并为你节省一步。我只是想让你了解这些机制。希望我没有让你感到困惑...... :(

As has been mentioned, none of this is necessary anyway--you can hand JS your non-quoted JSON and skip the parse because the JS interpreter will parse it as is and save you a step. I just wanted you to understand the mechanics. Hope I didn't confuse you... :(

JavaScript是唯一一种将JSON直接输出到上下文中而不再是字符串的语言实际上可以产生一些可行的东西。这是因为JSON语法借用了JavaScript文字语法的一个子集。

JavaScript is the only language in which the direct output of JSON into a context which makes it no longer a string could actually produce something workable. This is because JSON syntax borrows from a subset of the JavaScript literal syntax.

上下文就是其中的一切。例如,通过AJAX将JSON发送到JavaScript与您尝试为JavaScript提供一些JSON所显示的上下文不同。在AJAX的情况下,服务器对AJAX代码的响应已经是内存中的JS字符串。而将PHP回显到JS代码中则产生用于解析的语法JS引擎,因此需要引号使其成为一个字符串。

Context is everything in this. For example, sending JSON to JavaScript over AJAX is different than the context you showed for trying to give JavaScript some JSON. In the case of AJAX, the response from the server to the AJAX code is a JS string in memory already. Whereas having PHP echo into JS code produces syntax for parsing by the JS engine, thus needing quotes to make it a string.

这篇关于为什么json_encode后JSON.parse不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆