如何将列表[字符串]转换为Scala中的列表[对象]
我需要从一个list [String]
更改为list [MyObject]
在Scala中。
例如,JSON输入就像下面的
employee: {
name: "test",
employeeBranch: ["CSE", "IT", "ECE"]
}
输出一样,
Employee: {
Name: "test",
EmployeeBranch:[{"branch": "CSE"}, {"branch": "IT"}, {"branch": "ECE"}]
}
输入案例类:
Class Office(
name: Option[String],
employeeBranch: Option[List[String]])
输出案例类:
Class Output(
Name: Option[String],
EmployeeBranch: Option[List[Branch]])
case class Branch(
branch: Option[String])
这是要求。
I need to change from one List[String]
to List[MyObject]
in scala.
For example,JSON input is like below
employee: {
name: "test",
employeeBranch: ["CSE", "IT", "ECE"]
}
Output should be like this,
Employee: {
Name: "test",
EmployeeBranch:[{"branch": "CSE"}, {"branch": "IT"}, {"branch": "ECE"}]
}
Input case class:
Class Office(
name: Option[String],
employeeBranch: Option[List[String]])
Output case class:
Class Output(
Name: Option[String],
EmployeeBranch: Option[List[Branch]])
case class Branch(
branch: Option[String])
This is the requirement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不知道特定JSON库的详细信息的情况下,很难回答,但是
对象
可能表示为MAP
。因此,要将列表[String]
转换为list [MAP [String,String]]
您可以做到这一点:这给出了
应该转换为所需的JSON。
It is hard to answer without knowing details of the particular JSON library, but an
Object
is probably represented as aMap
. So to convert aList[String]
to aList[Map[String, String]]
you can do this:This gives
which should convert to the JSON you want.