【发布时间】:2013-03-07 11:32:32
【问题描述】:
是否可以通过 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代码吗?
-
您正试图在客户端(在 JavaScript 解析器内)解析 PHP。那是行不通的。您需要通过 PHP 页面的 POST 调用来请求数组,否则它将无法正常工作。
-
Uncaught SyntaxError: Unexpected token ;
-
@Allendar 对不起,我对你所说的有点困惑。你能引导我到有教程的页面吗?
-
@Allendar 嗯,当我通过 json 研究 php 数组到 javascript 数组时,这似乎是如何完成的。我想我需要寻找我们的更多选择来解决这个问题。
标签: php javascript json