我有一個擴展的 json 字串。
{"_id": {"oid": "59a47286cfa9a3a73e51e72c"}, "theaterId": {"numberInt": "101100"}, "location": {"address": {"street1": "340 XDW Market", "city": "Bloomington", "state": "MN", "zipcode": "12427"}, "geo": {"type": "Point", "coordinates": [{"$numberDouble": "-193.24565"}, {"$numberDouble": "144.85466"}]}}}
嘗試將上述 json 字串轉換為檔案以便將其插入 MongoDB。為此,我使用org.bson.Document.Document.parse(json_string)建構式。
但是我在決議后得到的檔案并沒有保留geo.coordinatearraylist 中的資料型別(檢查下面的檔案)。雖然它保留theaterId.
{
"_id": {
"oid": "59a47286cfa9a3a73e51e72c"
},
"theaterId": {
"numberInt": "101100"
},
"location": {
"address": {
"street1": "340 XDW Market",
"city": "Bloomington",
"state": "MN",
"zipcode": "12427"
},
"geo": {
"type": "Point",
"coordinates": [-193.24565, 144.85466]
}
}
}
這是 Document.parse() API 中的潛在問題嗎?
uj5u.com熱心網友回復:
您的欄位geo.coordinate以美元符號開頭$。在 theatreId 你有numberInt,而在坐標 - $numberDouble。
檢查檔案和這個問題,了解如何根據您的需要處理它。考慮到它看起來numberInt可以滿足您的需求,您可能只需要從欄位名稱中洗掉美元。
編輯:在深入研究這些檔案之后,您也提供的檔案{"numberInt": "101100"}不是具有資料型別的擴展 json,它只是具有該屬性的屬性和值的普通 json 物件。它需要{"$numberInt": "101100"}被擴展json。另一方面{"$numberDouble": "-193.24565"} 是延伸。資料型別不會丟失,它會被決議為List<Double>,因為我們知道每個元素Double的型別都可以重新構建資料型別。
如果您使用 at Document.toJson(),則它在引擎蓋下使用RELAXED輸出模式,它將在您看到它們時輸出坐標 - [-193.24565, 144.85466]。如果您提供EXTENDED輸出模式,例如這樣:
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
System.out.println(document.toJson(settings));
然后資料型別將從 java 型別重建回來,坐標將如下所示:
[{"$numberDouble": "-193.24565"}, {"$numberDouble": "144.85466"}]
總之, 沒有問題Document.parse("json"),但您提供給它的 json 可能有問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/432830.html
上一篇:無法以角度從API獲取回應
