如何解析一个Object数组,使其适合Bson文档 - Mongo Scala驱动程序

时间:2018-04-23 08:07:33

标签: json scala bson mongo-scala-driver

我正在使用Scala,Play框架和Mongo scala驱动程序来设置数据库。 我有json数据,我想在数据库中设置它,但我不知道如何格式化对象数组,以便它可以传递。

这是Json:

{
    "dateCreationProcess":"2018-04-18T16:05:21.244Z",
    "sci":false,
    "nomEntreprise":"a",
    "siren":"a",
    "dateCreation":"20/04/2018",
    "entreeRelation":"08/04/2018",
    "secteur":"AGRICULTEUR PETITE EXPLOITATION",
    "cotationBDF":"a",
    "montantPrivileges":"a",
    "fcc":"a",
    "ca":"a",
    "resultatBrut":"a",
    "ebe":"a",
    "totalBilan":"a",
    "fp":"a",
    "nbAssoc":"1",
    "assoc":
        [
            {
                "tiersAssoc":"a",
                "nom":"a",
                "prenom":"a",
                "birthday":"19/04/2018"
            },
            {
                "tiersAssoc":"b",
                "nom":"b",
                "prenom":"b",
                "birthday":"20/04/2018"
            }
        ],
        "cotation":"a",
        "fccAssociate":"a",
        "ficp":"a",
        "mntAcquisition":"",
        "codePostal":"",
        "typePret":"",
        "mntFinance":"",
        "mensualite":"",
        "loyerPrevisionnel":""
}

如您所见,“assoc”是一个对象数组。 对于我所做的所有其他领域:

val bsonDoc: Document = Document(
      "userId" -> new BsonString(userId),
      "processId" -> new BsonString(processId),
      "done" -> false,
      "sci" -> (uncompleteForm \ "sci").get.as[Boolean],
      "nomEntreprise" -> (uncompleteForm \ "nomEntreprise").get.as[String],
      "siren" -> (uncompleteForm \ "siren").get.as[String],
      "dateCreation" -> (uncompleteForm \ "dateCreation").get.as[String],
      "entreeRelation" -> (uncompleteForm \ "entreeRelation").get.as[String],
      "secteur" -> (uncompleteForm \ "secteur").get.as[String],
      "cotationBDF" -> (uncompleteForm \ "cotationBDF").get.as[String],
      "montantPrivileges" -> (uncompleteForm \ "montantPrivileges").get.as[String],
      "fcc" -> (uncompleteForm \ "fcc").get.as[String],
      "ca" -> (uncompleteForm \ "ca").get.as[String],
      "resultatBrut" -> (uncompleteForm \ "resultatBrut").get.as[String],
      "ebe" -> (uncompleteForm \ "ebe").get.as[String],
      "totalBilan" -> (uncompleteForm \ "totalBilan").get.as[String],
      "fp" -> (uncompleteForm \ "fp").get.as[String],
      "nbAssoc" -> (uncompleteForm \ "nbAssoc").get.as[String],
      "cotation" -> (uncompleteForm \ "cotation").get.as[String],
      "fccAssociate" -> (uncompleteForm \ "fccAssociate").get.as[String],
      "ficp" -> (uncompleteForm \ "ficp").get.as[String],
      "mntAcquisition" -> (uncompleteForm \ "mntAcquisition").get.as[String],
      "codePostal" -> (uncompleteForm \ "codePostal").get.as[String],
      "typePret" -> (uncompleteForm \ "typePret").get.as[String],
      "mntFinance" -> (uncompleteForm \ "mntFinance").get.as[String],
      "mensualite" -> (uncompleteForm \ "mensualite").get.as[String],
      "loyerPrevisionnel" -> (uncompleteForm \ "loyerPrevisionnel").get.as[String]
    )

如何在Bson文档中解析它?

2 个答案:

答案 0 :(得分:2)

编辑:

也许这样的事情会有所帮助吗?

import play.api.libs.json.{Json, Reads}

case class NestedObj(name: String)
val uncompleteForm= Json.parse("{ yourObj: .... {} }")
implicit val nestedObjRead: Reads[NestedObj] = Json.reads[NestedObj]

("assoc" -> new BsonArray((uncompleteForm \ 
  "assoc").get.as[List[NestedObj]].map(nestedObj => new 
BsonDocument("name" -> nestedObj.name))))

with reactivemongo: 如果你使用 "org.reactivemongo" %% "reactivemongo-play-json" % "0.11.14"你可以让这更容易。

import play.api.libs.json.{JsValue, Json, Reads}
import reactivemongo.bson.{BSONDocument, BSONDocumentWriter}
import reactivemongo.play.json.ImplicitBSONHandlers
import reactivemongo.play.json._
import play.api.libs.json._

case class YourObj(userId: String....)
val yourObjRead: Reads[YourObj] = Json.reads[YourObj]

val yourObjWriter: BSONDocumentWriter[YourObj] = 
  BSONDocumentWriter[YourObj] { obj =>
  ImplicitBSONHandlers.JsObjectWriter.write(Json.writes[YourObj](obj))
}

// this could already be done in you endpont when you receive the json
val json: JsValue             = Json.parse("{ yourObj: .... {} }")
val yourObjCaseClass: YourObj = yourObjRead.reads(json).fold(a => throw 
  new Error("sth went wrong parsing"), identity)

val bsonDoc: BSONDocument = yourObjWriter.write(yourObjCaseClass)

答案 1 :(得分:0)

我解决了这个问题:

val arrayAssoc = (uncompleteForm \ "assoc").as[List[Map[String, String]]]
val assoc = arrayAssoc.map((assoc: Map[String, String]) => Document(
      "tiersAssoc" -> assoc("tiersAssoc"),
      "nom" -> assoc("nom"),
      "prenom" -> assoc("prenom"),
      "birthday" -> assoc("birthday")
    ))


val bsonDoc: Document = Document(
      "userId" -> new BsonString(userId),
      "processId" -> new BsonString(processId),
      "done" -> false,
      "sci" -> (uncompleteForm \ "sci").get.as[Boolean],
      "nomEntreprise" -> (uncompleteForm \ "nomEntreprise").get.as[String],
      "siren" -> (uncompleteForm \ "siren").get.as[String],
      "dateCreation" -> (uncompleteForm \ "dateCreation").get.as[String],
      "entreeRelation" -> (uncompleteForm \ "entreeRelation").get.as[String],
      "secteur" -> (uncompleteForm \ "secteur").get.as[String],
      "cotationBDF" -> (uncompleteForm \ "cotationBDF").get.as[String],
      "montantPrivileges" -> (uncompleteForm \ "montantPrivileges").get.as[String],
      "fcc" -> (uncompleteForm \ "fcc").get.as[String],
      "ca" -> (uncompleteForm \ "ca").get.as[String],
      "resultatBrut" -> (uncompleteForm \ "resultatBrut").get.as[String],
      "ebe" -> (uncompleteForm \ "ebe").get.as[String],
      "totalBilan" -> (uncompleteForm \ "totalBilan").get.as[String],
      "fp" -> (uncompleteForm \ "fp").get.as[String],
      "nbAssoc" -> (uncompleteForm \ "nbAssoc").get.as[String],
      "assoc" -> assoc,
      "cotation" -> (uncompleteForm \ "cotation").get.as[String],
      "fccAssociate" -> (uncompleteForm \ "fccAssociate").get.as[String],
      "ficp" -> (uncompleteForm \ "ficp").get.as[String],
      "mntAcquisition" -> (uncompleteForm \ "mntAcquisition").get.as[String],
      "codePostal" -> (uncompleteForm \ "codePostal").get.as[String],
      "typePret" -> (uncompleteForm \ "typePret").get.as[String],
      "mntFinance" -> (uncompleteForm \ "mntFinance").get.as[String],
      "mensualite" -> (uncompleteForm \ "mensualite").get.as[String],
      "loyerPrevisionnel" -> (uncompleteForm \ "loyerPrevisionnel").get.as[String]
    )