如何在scala/play中将casbah mongodb列表转换为json

如何在scala/play中将casbah mongodb列表转换为json,scala,mongodb,playframework,casbah,Scala,Mongodb,Playframework,Casbah,我现在正在学习scala和mongodb,并使用这个游戏!框架,所以我在思考问题时犯了各种各样的错误。目前我有一个scala对象,它通过casbah返回mongodb查询返回的数据库对象列表,如下所示: object Alerts { def list() : List[DBObject]= { val collection = MongoDatabase.collection; val query = MongoDBObject.empty

我现在正在学习scala和mongodb,并使用这个游戏!框架,所以我在思考问题时犯了各种各样的错误。目前我有一个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
   }
val currentAlerts = Alerts.list()

var jsonList : List[JsValue] = Nil

// Iterate over the DBObjects and use to String to convert each to JSON
// and then parse that back into the list so we can use toJson on it later.
// MAD, but works.

for (dbObject <- currentAlerts) {
    jsonList ::=  Json.parse(dbObject.toString)
}

val result = Json.toJson(jsonList)
Ok(result).as("application/json")
。。。 }

在我的代码的其他地方,我希望输出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进行输出的正确方法是什么

编辑:

为了清楚起见,我真正想做的是

val listInJson = collection.find(query).sort(order).toJson
就像我会写一样

val listAsString = collection.find(query).sort(order).toString

我有一个可怕的解决方案,如下所示

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
   }
val currentAlerts = Alerts.list()

var jsonList : List[JsValue] = Nil

// Iterate over the DBObjects and use to String to convert each to JSON
// and then parse that back into the list so we can use toJson on it later.
// MAD, but works.

for (dbObject <- currentAlerts) {
    jsonList ::=  Json.parse(dbObject.toString)
}

val result = Json.toJson(jsonList)
Ok(result).as("application/json")
val currentAlerts=Alerts.list()
var jsonList:List[JsValue]=Nil
//迭代DBObjects并使用to字符串将每个对象转换为JSON
//然后将其解析回列表中,以便稍后使用toJson。
//疯狂,但有效。
对于(dbObject我有以下内容

def service() = Action {
 // connect
 val collection = MongoConnection()("someDB")("someCollection")
 // simply convert the result to a string, separating items with a comma
 // this string goes inside an "array", and it's ready to hit the road
 val json = "[%s]".format(
  collection.find(someQuery).toList.mkString(",")
 )

 Ok(json).as("application/json")
}你可以试试

com.mongodb.util.JSON.serialize(Alerts.list())

这将返回一个JSON数组和警报

您是否尝试了
JSON.toJson()
函数?()那么为什么您真的需要将数据转换为JSON?它以JSON的形式存储在数据库中(实际上是bson),您真的需要相同的备份吗?我想您可能只想根据所需的结构将数据复制到对象中,然后将其序列化为json…我需要将其作为json输出,供Web服务使用。您看到了吗?我想知道这是否适用于您…@Roger,您找到了一个优雅的解决方案吗?嘿,Roger,您找到了更好的方法吗o将casbah DBObject转换为play的JsValue?在获得
结果后,您将如何将其键值字段填充到地图中?这实际上是一个绝妙的想法!如果性能不重要(例如,在漂亮的打印中并不重要),这是完美的。谢谢。