MongoDB似乎返回BSON/JSON对象。使用MongoDB Java驱动程序将DBObject转换为POJO
我认为你肯定能够检索值作为字符串,整数等,然后可以保存为POJO。
我有一个DBObject(实例化为BasicDBObject)作为遍历列表的结果...(cur.next())。
使用JSON serlialiser/deserialiser来获取数据到POJO的唯一方法(除了使用某种持久性框架)?
我的方法是这样的:
public List<User> findByEmail(String email){
DBCollection userColl;
try {
userColl = Dao.getDB().getCollection("users"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace();}
DBCursor cur = userColl.find();
List<User> usersWithMatchEmail = new ArrayList<User>();
while(cur.hasNext()) {
// this is where I want to convert cur.next() into a <User> POJO
usersWithMatchEmail.add(cur.next());
}
return null;
}
编辑:这是很明显的,只是做这样的事情。
愚蠢的我,你可以调用get()上DBOBJECT和获得的价值。我会发布代码。 –
Ankur