[英]Map JSON to nested case class play framework
I need to receive Json data and bind it to a case class that contains other case class in parameters.我需要接收 Json 数据并将其绑定到在参数中包含其他案例类的案例类。
I don't even know what to write in the controller, there's no help in the documentation neither, I've look everywhere else and didn't find any answer.我什至不知道在控制器中写什么,文档中也没有帮助,我已经到处寻找,但没有找到任何答案。
Here are the case classes:以下是案例类:
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 !"
}
And here's what I've tried so far in the controller :这是我迄今为止在控制器中尝试过的:
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(""))
}
}
}
}
There is absolutely no log when I print.我打印时绝对没有日志。
According to the title I try to help you with the Json Mapping:根据标题,我尝试帮助您进行 Json 映射:
I created a ScalaFiddle , so you can try it yourself.我创建了一个ScalaFiddle ,所以你可以自己尝试一下。
That I could start I replaced the Date with a simple String, because you missed to have a formatter for Date (and it is not clear what Date you have).我可以开始用一个简单的字符串替换日期,因为您错过了日期的格式化程序(并且不清楚您有什么日期)。
So running this is not a success, because of the wrong Type:所以运行这个是不成功的,因为错误的类型:
Json.parse(json).validate[CompanyForm]
After fixing that everything works as expected: ScalaFiddle v.2修复一切正常后: ScalaFiddle v.2
Json.parse(json).validate[InfoForm]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.