我目前正在学习scala和mongodb并使用play!框架,所以当我思考问题时,我会犯各种各样的错误。目前我有一个 scala 对象,它返回通过 casbah 从 mongodb 查询返回的数据库对象列表,如下所示;
object Alerts {
def list() : List[DBObject]= {
val collection = MongoDatabase.collection;
val query = MongoDBObject.empty
val order = MongoDBObject("Issue Time:" -> -1)
val list = collection.find(query).sort(order).toList
list
}
...
}
在我的代码的其他地方,我希望输出 Json 中的对象列表 - 所以我有;
val currentAlerts = Alerts.list()
我想写的是类似的东西;
val resultingJson = currentAlerts.toJson
但是当我这样做时,我可以理解地得到以下错误;
value toJson is not a member of List[com.mongodb.casbah.Imports.DBObject]
我的问题是 - 将 com.mongodb.casbah.Imports.DBObject 列表转换为 Json 进行输出的正确方法是什么?
EDIT:
为了清楚起见,我真正想做的是相当于
val listInJson = collection.find(query).sort(order).toJson
就像我可以写的那样
val listAsString = collection.find(query).sort(order).toString
你可以试试
com.mongodb.util.JSON.serialize(Alerts.list())
这应该返回一个包含您的警报的 JSON 数组
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)