将json映射到嵌套的case类播放框架

时间:2018-04-17 15:59:11

标签: json scala playframework

我需要接收Json数据并将其绑定到包含参数中的其他case类的case类。

我知道我会因此而被投票但我甚至不知道在控制器中写什么,文档也没有帮助,我到处都看不到别的,没有找到任何答案,请我帮忙

以下是案例类:

case class InfoForm(nomEntreprise: String, siren: String, dateCreation: String, entreeRelation: String, secteur: String, cotationBDF: String, montantPrivileges: String, fcc: String, ca: String, resultatBrut: String, ebe: String, totalBilan: String, fp: String)

    object InfoForm {
      implicit val format = Json.format[InfoForm]
    }

    case class Associate(tiersAssoc: String, nom: String, prenom: String, birthday: Date)

    object Associate {
      implicit val assocFormat = Json.format[Associate]
    }

    case class AssociateForm(nbAssoc: Int, cotation: String, assoc: Seq[Associate], fccAssociate: String, ficp: String)

    object AssociateForm {
      implicit val format = Json.format[AssociateForm]
    }

    case class OperationForm(mntAcquisition: String, codePostal: String, typePret: String, mntFinance: String, mensualite: String, loyerPrevisionnel: String)

    object OperationForm {
      implicit val format = Json.format[OperationForm]
    }

    case class CompanyForm(sci: Boolean, infoForm: InfoForm, associateForm: AssociateForm, operationForm: OperationForm)

    object CompanyForm {
      implicit val format = Json.format[CompanyForm]
    }

JSON:

{
    "sci": true,
    "nomEntreprise": "nom entreprise",
    "siren": "siren",
    "dateCreation": "1977-04-22T01:00:00-05:00",
    "entreeRelation": "1977-04-22T01:00:00-05:00",
    "secteur": "un secteur",
    "cotationBDF": "cotation",
    "montantPrivileges": "montant",
    "fcc": "fcc",
    "ca": "ca c est un option attention",
    "resultatBrut": "resultat",
    "ebe": "ebe",
    "totalBilan": "totalBilan",
    "fp": "fp",
    "nbAssoc": 1,
    "cotation": "une chaine",
    "assoc": [
        {
            "tiersAssoc": "une chaine",
            "nom": "name",
            "prenom": "prenom",
            "birthday":"1977-04-22T01:00:00-05:00"
        }
    ],
    "fccAssociate": "une autre chaine",
    "ficp": "encore une autre chaine",
    "mntAcquisition": "montant acquisition",
    "codePostal": "code postal",
    "typePret": "typePret",
    "mntFinance": "montant finance",
    "mensualite": "string",
    "loyerPrevisionnel": "derniere !"
}

以下是我迄今为止在控制器中尝试过的内容:

def setCompanyForm(id: String) = {
    Errors.collect {
      silhouette.SecuredAction.async { implicit request =>
        Errors.handle {
          val companyForm = request.body.asJson
          companyForm match {
            case Some(json) => println(Json.fromJson[CompanyForm](json))
            case None => println("rien")
          }

          Future.successful(Ok(""))
        }
      }
    }
  }

我打印时绝对没有日志。

1 个答案:

答案 0 :(得分:0)

根据标题,我试着帮助你使用Json Mapping:

我创建了ScalaFiddle,因此您可以自己尝试。

我可以开始用简单的String替换Date,因为你错过了Date的格式化程序(并且不清楚你有什么日期)。

所以运行这个并不成功,因为错误的Type:

xi += 0.01; x = xl; xl = x + xi; yl = y; line[0] = x; line[1] = y; line[2] = xl; line[3] = yl; lines.add(line);`

确定一切按预期工作后:ScalaFiddle v.2

Json.parse(json).validate[CompanyForm]