当我试图将数组转换为json时,我在PHP遇到了这个问题我有一个递归函数,构建数组,以jSon格式编码。
这是数组:
$data = array(
array('id' => 1, 'parent_id' => null, 'text' => 'lorem ipsum'),
array('id' => 2, 'parent_id' => 1, 'text' => 'lorem ipsum1'),
array('id' => 3, 'parent_id' => 1, 'text' => 'lorem ipsum2'),
array('id' => 4, 'parent_id' => 2, 'text' => 'lorem ipsum3'),
array('id' => 5, 'parent_id' => 3, 'text' => 'lorem ipsum4'),
array('id' => 6, 'parent_id' => null, 'text' => 'lorem ipsum5'),
);
这是我的职责:
function trataArray($data) {
$itemsByReference = array();
// Build array of item references:
foreach ($data as $key => &$item) {
$itemsByReference[$item['id']] = &$item;
// Children array:
$itemsByReference[$item['id']]['children'] = array();
// Empty data class (so that json_encode adds "data: {}" )
$itemsByReference[$item['id']]['data'] = new StdClass();
}
// Set items as children of the relevant parent item.
foreach ($data as $key => &$item)
if ($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
$itemsByReference [$item['parent_id']]['children'][] = &$item;
// Remove items that were added to parents elsewhere:
foreach ($data as $key => &$item) {
if ($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
unset($data[$key]);
}
// Encode:
return $data;
}
这是我的json,但是这个json有错误:
{
"0": // Problem here... T_T
{
"id": 1,
"parent_id": null,
"name": "lorem ipsum",
"children": [
{
"id": 2,
"parent_id": 1,
"name": "lorem ipsum1",
"children": [
{
"id": 4,
"parent_id": 2,
"name": "lorem ipsum3",
"children": [],
"data": {}
}
],
"data": {}
},
{
"id": 3,
"parent_id": 1,
"name": "lorem ipsum2",
"children": [
{
"id": 5,
"parent_id": 3,
"name": "lorem ipsum4",
"children": [],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
"5": // And here... T_T
{
"id": 6,
"parent_id": null,
"name": "lorem ipsum5",
"children": [],
"data": {}
}
}
字符串"0":和"5":在json标记上导致错误。我想尝试使用preg_replace删除它,但我对正则表达式的工作很糟糕。有人可以帮我……: D
谢谢你们了!
我有个主意…:)
我在寻找测试东西的方法…: D我有一个想法,从插件中获得JSON并将其编码为一个数组并打印它,看看我需要给JSON编码给我正确的东西。
他们复制了它:
Array
(
[0] => stdClass Object
(
[id] => 1
[text] => City
[children] => Array
(
[0] => stdClass Object
(
[id] => 11
[text] => Wyoming
[children] => Array
(
[0] => stdClass Object
(
[id] => 111
[text] => Albin
)
[1] => stdClass Object
(
[id] => 112
[text] => Canon
)
[2] => stdClass Object
(
[id] => 113
[text] => Egbert
)
)
)
[1] => stdClass Object
(
[id] => 12
[text] => Washington
[state] => closed
[children] => Array
(
[0] => stdClass Object
(
[id] => 121
[text] => Bellingham
)
[1] => stdClass Object
(
[id] => 122
[text] => Chehalis
)
[2] => stdClass Object
(
[id] => 123
[text] => Ellensburg
)
[3] => stdClass Object
(
[id] => 124
[text] => Monroe
)
)
)
)
)
)
JSON基于对象,我认为错误就在那里,我试图与数组工作并转换它,当数据是一个对象。
有人知道如何创造这样的东西吗?只是一种我能想到的方法…谢谢大家…
: DDD
根据jslint.com,这是有效的JSON。
您是如何看到错误的?
简单地将JSON直接粘贴到<script>
块并在浏览器中加载会在IE和Chrome中引发错误,然而,将相同的JSON分配给变量首先可以防止任何错误消息。闭包编译器证实了这些发现;当您的裸JSON通过编译运行时,它会失败。当引入一个简单的变量赋值时,一切都可以正常编译。
似乎有一些JS的词法规则,我不知道这是影响这;遗憾的是,我不确定根本原因是什么。
另外,我不确定您是如何生成结果JSON的。我的意思是,我知道你在使用json_encode()
,但无论我对数组键做什么,我都无法用PHP中的数组复制你的输出
我想模仿这个:
{"0": "foo"}
然而,在PHP中使用数字数组键将意味着结果JSON将是一个数组;例如:
json_encode( array(0 => 'foo') );
> (string) ["foo"]
json_encode( array("0" => 'foo') );
> (string) ["foo"]
唯一的方法,我可以重新创建你的JSON是通过做以下:
$x = new stdClass;
$x->{"0"} = 'foo';
json_encode($x);
> (string) {"0": "foo"}
所以,我认为你的例子要么是不完整/误导/错误的,要么你发现了PHP中一些极其晦涩的错误。
在键中引入空格,以及像下面这样的其他排列,也不会生成JSON:
json_encode( array("0 " => 'foo') );
> (string) {"0 ": "foo"}
json_encode( array(" 0" => 'foo') );
> (string) {" 0": "foo"}
json_encode( array("00" => 'foo') );
> (string) {"00": "foo"}
json_encode( array(false => 'foo') );
> (string) ["foo"]
json_encode( array(null => 'foo') );
> (string) {"": "foo"}
json_encode( array("" => 'foo') );
> (string) {"": "foo"}
不知道发生了什么
试试这样:
var $counter = 0;
foreach ($data as $key => &$item) {
$itemsByReference[$counter] = &$item;
// Children array:
$itemsByReference[$counter]['children'] = array();
// Empty data class (so that json_encode adds "data: {}" )
$itemsByReference[$counter]['data'] = new StdClass();
$counter++;
}
这样你就不会创建一个关联数组,并且在编码之后,它不应该给你"0"answers"5"作为键。它将是一个对象数组
这是因为您不应该将0到5定义为字符串。你应该把它定义为int,所以你甚至不需要给它命名。这样的:
[
{
// This will be 0
},
{
// This will be 1, and so on
}
]