MongoDB
 SQL >> Database >  >> NoSQL >> MongoDB

Libreria BSON per Java

Puoi usare il driver MongoDB per Java per salvare un oggetto BSON e convertirlo in String, poi avvolgerlo in JSONObject.

BasicDBObject obj = new BasicDBObject();
obj.put("name", "Matt");
obj.put("date", new Date());

String bsonString = obj.toString();

Puoi quindi creare un JSONObject e leggere l’attributo date, che verrà restituito in un formato compatibile con BSON.

JSONObject newObject = new JSONObject(bsonString);
System.out.println(newObject.get("date"));

Il risultato sarà simile a:

{"$date":"2012-08-10T05:22:53.872Z"}