0

I`m trying to convert List to Json String, here is my code

  def getProductAsJson(txt:String):String={
                var products=new ListBuffer[Product]()


                 val conn = DB.getConnection()
                 try {
                    val stmt = conn.createStatement
                    val q="SELECT * from m_products where pro_name like '"+txt+"%' "
                    println(q);
                    val rs = stmt.executeQuery(q)
                     while (rs.next()) {
                      products+=Product(Some(rs.getInt("idproduct")),rs.getString("pro_name"),rs.getBigDecimal("pro_retprice"),
                      rs.getString("pro_description"),rs.getString("pro_brand"),rs.getString("pro_type"),rs.getString("pro_sup"),rs.getString("pro_supref"),rs.getBigDecimal("pro_supprice"),rs.getBigDecimal("pro_markup"),Some(rs.getString("pro_imgpath")),rs.getInt("pro_active"));
                    }
                } finally {
                    conn.close()
                }

                println(Json.toJson(products.toList).toString)
                return Json.toJson(products.toList).toString

           } 

but i got this error

No Json serializer found for type List[models.Product]. Try to implement an implicit Writes or Format for this type.

1
  • If you use Product class only fo creating string, you may construct Json in while loop manualy like creating Product. Commented Feb 12, 2016 at 16:11

1 Answer 1

3

Have you implemented a Writes for your models.Product? If not, add something like this:

import play.api.libs.json._

implicit val productWrites = Json.writes[Product]

Then make sure that implicit productWrites is in scope where you call Json.toJson.

See https://www.playframework.com/documentation/2.4.x/ScalaJsonCombinators#Writes and https://www.playframework.com/documentation/2.4.x/ScalaJsonInception#writes

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.