I want to insert the JSON string directly into mongo db, without parsing
String json = "{ 'name' : 'Vince' , " +
"'website' : 'howtodoinjava.com' , " +
"'address' : { 'addressLine1' : 'Some address' , " +
"'addressLine2' : 'Cardiff CF116LW' , "
"'addressLine3' : 'Cardiff, UK'} }"
DBObject dbObject = (DBObject)JSON.parse(json);
collection.insert(dbObject);
In the above example i don't want to use this line
DBObject dbObject = (DBObject)JSON.parse(json);
Is there any out of the box solution for the same ?
The insert method takes one or more DBObject
as parameters, so no you cannot pass a String
instead.
There are a few ways to obtain a DBObject
;
BasicDBObject
inherits from HashMap
) Map
and use the BasicDBObject constructor that takes a Map
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.