【问题标题】:MongoDB object id issue in json outputjson输出中的MongoDB对象ID问题
【发布时间】:2015-12-27 09:13:10
【问题描述】:

我必须通过PHPMongoDB 读取一些数据。 当我将MongoDB 结果转换为Json 时,我得到如下结果:

{    
    "records":
    [            
        {
            "_id":
            {
                "id": "567f18f21e2e328206d3d91e"
            },
            "title": "test",
            "price": 5000,
            "date": "1394-09-05 11:30",
            "images":
            [
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d91f"
                    },
                    "url": "a"
                },
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d920"
                    },
                    "url": "x"
                },
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d921"
                    },
                    "url": "c"
                }
            ]
        }
    ]
}

如您所见,我们有一个如下所示的 id:

"_id":
 {
     "$id": "567f18f21e2e328206d3d91e"
  },

当我想用 Java 解析这个 json 时,它给我带来了一些问题,我想把它转换成这样的东西:

{    
    "records":
    [
        {

            "id": "567f18f21e2e328206d3d91e"           
            "title": "test",
            "price": 5000,
            "date": "1394-09-05 11:30",
            "images":
            [
                {
                    "id": "567f18f21e2e328206d3d91f",
                    "url": "a"
                },
                {
                    "id": "567f18f21e2e328206d3d920",
                    "url": "x"
                },
                {
                    "id": "567f18f21e2e328206d3d921",
                    "url": "c"
                }
            ]
        }
    ]
}

我该怎么做?
我不能使用foreach 方法来编辑这个数组,因为我有无限的子数组

【问题讨论】:

    标签: php json mongodb phalcon


    【解决方案1】:

    首先decode json 数据并将其转换为数组。从转换后的数组中,您可以格式化您的预期模式。 array_map可以帮到你。

    $data = json_decode($old_json_data, true);
    
    $new_data_format = [];
    if(isset($data['records'])){
        $data = $data['records'];
        $new_data_format = array_map(function($val){
            return [
                'id' => $val['_id']['id'],
                'title' => $val['title'],
                'price' => $val['price'],
                'date' => $val['date'],
                'images' => array_map(function($v){
                    return [
                        'id' => $v['_id']['id'],
                        'url' => $v['url'],
                    ];
                }, $val['images'])
            ];
        }, $data);
    }
    
    $new_data_format = json_encode(['records' => $new_data_format]);
    

    希望对你有帮助。

    【讨论】:

    • 非常感谢,但正如我提到的,我在这个数组中有无限的子数组,它们都有“_id”,我应该替换它们。这只是一个样本。
    • 用递归函数可以做到这一点吗?
    • @AliHasanzade,避免递归函数的原因是什么?
    猜你喜欢
    • 2017-05-24
    • 2014-02-06
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 2014-08-06
    • 2014-05-27
    相关资源
    最近更新 更多