【发布时间】:2017-09-21 12:38:26
【问题描述】:
我通过 ajax 将数据从 php 传递到 js。我在传输之前使用 json_encode。这里是例子:
$data = [
[
"path" => "/TestMenu1",
"component" => 'test1',
"children" => [[
"path" => "/api/sub/route",
"component" => "test2",
]]
],[
"path" => "/api/findme/2",
"component" => "test3",
]
];
从客户端我得到这个没有任何问题:
[
{
"path": "/TestMenu1",
"component": "test1",
"children": [
{
"path": "/api/sub/route",
"component": "test2"
}
]
},
{
"path": "/api/findme/2",
"component": "test3"
}
]
但我需要组件是变量名而不是字符串,例如:
[
{
"path": "/TestMenu1",
"component": test1,
"children": [
{
"path": "/api/sub/route",
"component": test2
}
]
},
{
"path": "/api/findme/2",
"component": test3
}
]
这可能吗?如果是,我该怎么做?
【问题讨论】:
-
JSON.encode不是函数。在PHP或JavaScript中。 -
是的,你是对的@Script47 它实际上是 json_encode()。
-
目前无法实现您的要求。您必须编写自己的 JSON 编码版本,并以某种方式标记哪些值是 JS 使用的变量。这将破坏 JSON 的目的和标准化使用,因此您不能再将其称为 JSON。
标签: javascript php arrays json ajax