Php 存在JSON有效密钥
我知道,为什么来自MongoDB的JSON编码数据通过PHP呈现为:Php 存在JSON有效密钥,php,json,mongodb,Php,Json,Mongodb,我知道,为什么来自MongoDB的JSON编码数据通过PHP呈现为: { "53a6e02221360e263e000000": { "_id": { "$id": "53a6e02221360e263e000000" }, "title": "1" }, "53a6e02221360e263e000001": { "_id": { "$id": "53a6e0
{
"53a6e02221360e263e000000": {
"_id": {
"$id": "53a6e02221360e263e000000"
},
"title": "1"
},
"53a6e02221360e263e000001": {
"_id": {
"$id": "53a6e02221360e263e000001"
},
"title": "2"
}
}
不好,但是:
[
{
"id": "53a6e02221360e263e000000",
"title": "1"
},
{
"id": "53a6e02221360e263e000001",
"title": "2"
}
]
好吗?
你知道吗?两者都是有效的JSON
PHP代码示例:
// MongoDB init before it
$cursor = $mongo->db->collection->find();
// For not large data
$result = iterator_to_array( $cursor );
$json = json_encode($result);
var_dump($json);
你说的坏是什么意思?你得到了什么错误?@wavemode这意味着键显然不代表目标场。例如{'00001':'tst1'},{'00002':'tst2'}},通过[{'id':'00001','title':'tst1'},{'id':'00002','title':'tst2'}]我想我们还是不明白你的问题是关于什么的。你说坏是什么意思?插入它不起作用?使用JSON表单是一种不好的做法?@wdberkeley是的,使用JSON表单是一种不好的做法吗?是的。数据作为键是一种糟糕的形式,因为如果您不知道要操作的键的名称,它将严重阻碍您执行CRUD操作和聚合的能力。