dongtuojuan8998 2015-11-07 22:38
浏览 51

PHP json_encoded数组失败mongoimport

I have a PHP script that dumps a list of people using json_decode()

foreach ( $personArray as $person ) {               
    $currentPosition = $person->loadCurrentPosition();
    $one[ FIELD_NAME ] = $person->getName();
    $one[ FIELD_ID ] = $person->getId();
    $one[ FIELD_ORGANIZATION_ID ] = $currentPosition->organization->getId();
    $one[ FIELD_ORGANIZATION_NAME ] = $currentPosition->organization->getName();
    $result[] = $one;
}
$jsonResult = json_encode( $result  );

I put the result into a file called 'people.json' :

[
    {
        "name": "Bobby Brown",
        "id": 32632,
        "organizationid": 40492,
        "organizationname": "Exinda Networks"
    },
    {
        "name": "Billy Bob",
        "id": 32633,
        "organizationid": 29824,
        "organizationname": "Desire2Learn"
    }
]

I used mongoimport (mongodb v3.0.7):

mongoimport --db test --collection people --drop --file people.json

I get the following error and 0 documents imported:

2015-11-07T17:02:35.461-0500    Failed: error unmarshaling bytes on document #0: JSON decoder out of sync - data changing underfoot?

I remove the leading square bracket '[' and trailing ']' in the file and try mongoimport again:

2015-11-07T17:13:37.575-0500    Failed: error processing document #2: invalid character ',' looking for beginning of value

I remove the ',' between the two JSON objects in the file and the import works:

2015-11-07T17:16:12.162-0500    imported 2 documents

Is json_encode() incorrectly encoding?

Will one of the json_encode() options result in a format that mongoimport likes?

  • 写回答

1条回答 默认 最新

  • douweilei2307 2015-11-08 00:46
    关注

    add "--jsonArray" option to mongoimport

    big thanks to @amber4478 who answered this question Mongoimport of json file

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法