-->

Convert from MySQL query result or LIST to JSON

2019-07-25 15:18发布

问题:

I am using anorm with play-scala and have execute queries using:

val result = SQL("SELECT * FROM users)()

What I have been doing is the following:

 val queryResult = SQL(query)().map(result =>
  result.asList
  ).toList

However, I have realized that instead of converting it to a list, I want to actually receive JSON. Since I may change the query, I would like the ability for a JSON serializer that takes in any result and understands the column names and convert them and the results into JSON.

EDIT: I'm using play 2.3.10 with scala version 2.11.6. I'm trying to map the SqlResult's metadata column names to an object or rather a json parser. I was able to get the SqlResult's metadata as

List(MetaDataItem(ColumnName(x),false, java.lang.Integer), MetaDataItem(ColumnName(y),.....

Thanks