将JSON PHP数组转换为Javascript

5
能否通过json_encode将此php数组转换为JSON? 因为这个php数组被称为$ data,当我这样做时...
var myJson = <?php echo json_encode($data) ?>;
console.log(myJson);

在JavaScript中,它会给我报错。我想知道JSON对于什么类型的PHP数组有限制或者没有限制。
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Tanner, C. kickoff 60 yards to the DU5, Butler, L return 14 yards to the DU19 (Johnson, I.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT35
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => K
                        )

                    [drive] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass complete to Vernon, C for 6 yards to the DU25 (Thomas, Je.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU19
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [1] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass incomplete to Scott, D.
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU25
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 4
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [2] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass complete to Scott, D for 6 yards to the DU31, 1ST DOWN DU (Thomas, Je.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU25
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 4
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [3] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass complete to Scott, D for no gain to the DU31 (Sweeting, R.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU31
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [4] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass incomplete to Vernon, C.
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU31
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [5] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S sacked for loss of 4 yards to the DU27 (Gotsis, A.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU31
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [6] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Monday, W punt 53 yards to the GT20, Golden, J. return 8 yards to the GT28 (Reeves, D).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU27
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 14
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => U
                        )

                    [drive] => 2
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Smith, O. rush for 8 yards to the GT36 (Brown, Ky).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT28
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [1] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Sims, D. rush for 6 yards to the GT42, 1ST DOWN GT (Sink, N).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT36
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 2
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [2] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. pass incomplete to Smith, O..
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT42
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 3
                )

            [3] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for 2 yards to the GT44 (Anunike, K).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT42
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [4] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Greene, J. rush for 6 yards to the 50 yardline (Canty, W).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT44
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 8
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [5] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Smith, O. rush for 16 yards to the DU34, 1ST DOWN GT (Canty, W;France, C).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT50
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 2
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [6] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Smith, O. rush for 5 yards to the DU29 (France, C).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU34
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [7] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Laskey, Z. rush for 3 yards to the DU26 (France, C).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU29
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 5
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [8] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for 3 yards to the DU23, 1ST DOWN GT (France, C).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU26
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 2
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [9] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. pass complete to Smith, O. for 21 yards to the DU2, 1ST DOWN GT (Butler, L).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU23
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 3
                )

            [10] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for 1 yard to the DU1 (Brown, Ky;Foxx, J).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU02
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [11] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for no gain to the DU1 (Cockrell, R).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU01
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [12] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for 1 yard to the DU0, TOUCHDOWN, clock 06:51.
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU01
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [13] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Tanner, C. kick attempt good.
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU03
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => X
                        )

                    [drive] => 3
                )

            [14] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Tanner, C. kickoff 54 yards to the DU11, Butler, L return 23 yards to the DU34 (Harrell, A.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT35
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => K
                        )

                    [drive] => 3
                )

        )
)

你能发布生成的JavaScript代码吗? - calebds
您正在试图在客户端(JavaScript解析器内部)解析PHP。这是行不通的。您需要从PHP页面通过POST调用请求数组,否则它无法工作。 - user1467267
未捕获的语法错误:意外的令牌< @ 行 var myJson = <?php echo json_encode($data) ?>; - ealeon
@Allendar,很抱歉,我有点困惑你所说的内容。能否指导我找到一个有教程的页面? - ealeon
@Allendar 嗯,这似乎是我通过 JSON 将 PHP 数组转换为 JavaScript 数组时研究的方法。我想我需要寻找更多关于如何处理这个问题的选项。 - ealeon
你看起来很困惑。你不能把PHP语法放到JavaScript解析器中,这是不可能的。即使客户端电脑上安装了PHP,JavaScript仍然无法访问命令行工具或系统资源来触发PHP调用。如果你看一下我的答案,你会发现需要在PHP服务器上调用一个PHP文件才能获取实际数据。它需要被“解析”。 - user1467267
5个回答

9

首先,您需要在已安装PHP的Apache服务器上创建一个PHP文件。可以像这样创建一个文件:

localhost:8888/myfile.php

<?php
    $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

    echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}
?>

那么你的 JavaScript 代码(在这个例子中我使用了 jQuery):

$.getJSON('http://localhost:8888/myfile.php', function(data) {
    console.log(data);
});

这是将PHP数组传递给JavaScript的起点。

那么,在这种情况下,数据有我的PHP数组?那么我能否通过JavaScript访问数组,例如data [0] [0] .text? - ealeon
是的,PHP文件将打印出原始的JSON“序列化/编码”数据。然后JavaScript(jQuery)将读取原始字符串并将其解码回JavaScript内部的data对象中。编码/解码可以双向工作。 - user1467267

4

正如@Allendar所说,你不能在JS文件中嵌入PHP。但是,你可以在JS文件中添加一个函数来加载JSON数据,然后将该数据嵌入到PHP文件的script标签中。

示例.js:

var loadJsonFromPHP = function(json) {
    console.log(json);
}

example.php:

<?php
    $data = array("some", "test", "data");
?>
<html>
    <head>
        <script src="example.js"></script>
        <script>
            loadJsonFromPHP(<?php echo json_encode($data) ?>);
        </script>
    </head>
    <body></body>
</html>

编辑:假设您只需要在页面加载时将数据传递给JS一次,那么您可以跳过发起AJAX请求的步骤。


是的,但它仍然需要文件作为在PHP服务器上运行的PHP脚本。我不确定,但似乎ealeon似乎没有理解它如何交织在一起的想法。我希望他能解决这个问题 :) - user1467267

4
将PHP生成的JSON传递给Javascript并读取 var stuff = <?php print json_encode($datajson); ?>; var arr = new Array(); arr= JSON.parse(stuff); document.write( arr[0].cust_code ); 将PHP生成的JSON数据传递给Javascript,并通过JSON.parse()方法将其解析成数组,然后可以访问该数组中的元素。在此示例中,我们输出第一个元素的cust_code属性。

0

我从未尝试过像这样的事情,但我认为您遇到了问题,因为json_encode返回一个已编码的JSON字符串。然后,您需要在Javascript端解码此字符串。尝试类似以下的内容:

var myJson = JSON.parse(<?php echo json_encode($data) ?>);
console.log(myJson);

我不知道为什么这个答案有负评,但它确实解决了我的问题。当我得到一个js对象并将其Json.stringify时,我得到了我想要的结果。 - Michael Shang
这是错误的,因为在 PHP 中,json_encode 返回一个 JSON 编码的字符串,但在 JS 中,由于没有引号包裹 <?php ...?>,它会打印出 JS 代码。 - François Huppé
另一方面,我明白为什么它解决了你的问题,因为如果你添加引号,这是最好的解决方案:var myJson = JSON.parse('<?php echo json_encode($data) ?>'); - François Huppé

-1

JSON 可以处理任何类型的数组(尽管它会将关联数组转换为对象)。你可能面临的问题是,当数据仅在 Javascript 上可用时,你正在尝试使用 PHP 输出。

澄清一下:页面加载后,PHP 无法执行任何操作。只有 JavaScript 能够在客户端处理事情,PHP 仅在服务器上工作,并不知道客户端的状态。


这是一个注释,而不是解决方案。 - user1467267

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接