简体   繁体   English

Scala:没有找到Object类型的Json序列化器。 尝试为此类型实现隐式的Writes或Format

[英]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.

相关问题 找不到类型为Seq [(String,String)]的Json序列化程序。 尝试为此类型实现隐式写入或格式 - No Json serializer found for type Seq[(String, String)]. Try to implement an implicit Writes or Format for this type 错误消息:尝试实现此类型的隐式写入或格式 - Error message: Try to implement an implicit Writes or Format for this type Scala Play Json隐式写入类型不匹配 - Scala Play Json implicit writes type mismatch Scala:未找到任何类型的Json序列化器 - Scala: No Json serializer found for type Any 没有为类型model.MpMember找到ByteString反序列化器。 尝试为此类型实现隐式ByteStringDeserializer - No ByteString deserializer found for type models.MpMember. Try to implement an implicit ByteStringDeserializer for this type 在Scala Play应用程序中获得“找不到类型为scala.concurrent.Future ...的Json序列化程序”异常 - Getting “No Json serializer found for type scala.concurrent.Future…” exception in Scala Play application 通用类型的Scala Play框架json序列化器 - Scala Play framework json serializer for generic type Playframeworks json写隐式要求显式类型,为什么? - Playframeworks json Writes implicit requires explicit type, why? 未找到 Seq[Foo] 类型的 Json 序列化程序 - No Json serializer found for type Seq[Foo] 找不到scala.concurrent.Future类型的Json序列化器[List [models.Product]] - No Json serializer found for type scala.concurrent.Future[List[models.Product]]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM