2015-11-07 21 views
0

我有轉儲的使用json_decode()人PHP json_encoded陣列失效mongoimport

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 ); 

我結果放入一個名爲 'people.json' 列表的PHP腳本:

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

我用mongoimport(MongoDB的v3.0.7):

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

我碰到下面的錯誤和0文檔導入:

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

予刪除前面的方括號「[」和尾隨「]」的文件中,並mongoimport再次嘗試:

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

予刪除該文件在兩個JSON對象之間的「」和進口工程:

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

json_encode()錯誤地編碼?

json_encode()選項中的一個會導致mongoimport喜歡的格式嗎?

+0

使用jsonlint.com檢查您的JSON的有效性。 – aldrin27

+0

感謝@ aldrin27爲PHP json_encode()文件jsonlint.com說「有效的JSON」。對於修改後的mongoimport閱讀OK jsonlint.com說「解析錯誤」,所以它顯示它是mongoimport –

回答