[英]Scala : No Json serializer found for type Object. Try to implement an implicit Writes or Format for this type
I have two functions, one returns a single user based on his id and the second returns multiple users based on their ids, 我有两个函数,一个函数根据其ID返回一个用户,第二个函数根据其ID返回多个用户,
The first function return this array : User[id=1, name="name1", lastName= "lastname1"]
第一个函数返回此数组: User[id=1, name="name1", lastName= "lastname1"]
The second function return this: [User [id=1, name="name1", lastName= "lastname1"], User [id=2, name="name2", lastName= "lastname2"]]
第二个函数返回此信息: [User [id=1, name="name1", lastName= "lastname1"], User [id=2, name="name2", lastName= "lastname2"]]
My first scala function is as follow and the json is well returned and i have no error: 我的第一个scala函数如下,并且json返回正确,并且我没有错误:
Option(Try(id.toLong) match {
case Success(id) => User.getUserById(1)
}) match {
case Some(user) => Ok(Json.toJson(user) )
case None => NotFound
}
But my second scala function returns an error (see my title) : 但是我的第二个scala函数返回一个错误(请参阅我的标题):
Option(Try(id.toList) match {
case Success(id) => User.getUsersByIds(id) // id list is 1,2
}) match {
case Some(users) => Ok(Json.toJson(users) )
case None => NotFound
}
My getUsersByIds function : 我的getUsersByIds函数:
public static synchronized ArrayList<User> getUsersByIds(List usersIDs) {
String strSQL = "SELECT * FROM User WHERE ID IN" + usersIDs) ;
ArrayList<User> userList = getArrayList(strSQL);
return userList
}
How can i return the right json format to get all my users ? 如何返回正确的json格式以获取所有用户? I need to add a map ? 我需要添加地图吗? if so, how ? 如果是这样,如何?
thank you 谢谢
By default, Play only has serializers for the Scala collection. 默认情况下,Play仅具有Scala集合的序列化器。 This should make it work: 这应该使其工作:
import scala.collection.JavaConverters._
Json.toJson(users.asScala)
好的,我找到了:.asScala解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.