0

我正在使用 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 文档中将其解析为可接受的?

4

2 回答 2

2

edit:

maybe something like this can help?

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: if you use "org.reactivemongo" %% "reactivemongo-play-json" % "0.11.14" you can make this easier.

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)
于 2018-04-23T09:31:53.067 回答
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]
    )
于 2018-04-23T12:47:21.070 回答