如何使用play框架将列表数据转换为scala中的Json?

时间:2014-10-11 07:15:30

标签: json scala playframework-json

我正在使用Play框架和Scala。

我有以下列表 -

 List(
    (C: - read,1412750402124,46552070),
    (C: - write,1412750402124,433057), 
(E: - read,1412750402124,5435),  
(E: - write,1412750402124,1728),
(F: - read,1412750402124,5435),  
(F: - write,1412750402124,1728), 
(C: - read,1412750402125,46552071), 
(C: - write,1412750402125,433060),
(E: - read,1412750402125,5445), 
(E: - write,1412750402125,1730),
(F: - read,1412750402125,5450),  
(F: - write,1412750402125,1428)
)

我想要关注输出 -

    key:[
{"name":"C:-read",
data:[[1412750402124,46552070],[1412750402125,46552071]...]
},
{
"name":"C:-write",
data:[[1412750402124,433057],[1412750402125,433060]...]
},
{
"name":"E:-read",
data:[[1412750402124,5435],[1412750402125,5445]...]
},
{
"name":"E:-write",
data:[[1412750402124,1728],[1412750402124,1730]...]
},
{
"name":"F:-read",
data:[[1412750402124,5435],[1412750402125,5450]...]
},
{
"name":"F:-write",
data:[[1412750402124,1728],[1412750402124,1428]...]
}
]

如何使用scala获得以上输出?

1 个答案:

答案 0 :(得分:1)

输入List无法编译,缺少一些信息,所以我做了一些假设。

val lst = List(
  ("C: - read", 1412750402124L, 46552070),
  ("C: - write", 1412750402124L, 433057),
  ("E: - read", 1412750402124L, 5435),
  ("E: - write", 1412750402124L, 1728),
  ("F: - read", 1412750402124L, 5435),
  ("F: - write", 1412750402124L, 1728),
  ("C: - read", 1412750402125L, 46552071),
  ("C: - write", 1412750402125L, 433060),
  ("E: - read", 1412750402125L, 5445),
  ("E: - write", 1412750402125L, 1730),
  ("F: - read", 1412750402125L, 5450),
  ("F: - write", 1412750402125L, 1428))

由于结果按第一个字符串分组(例如" C: - read"),第一步是进行分组:

scala> val groupedData= lst.groupBy(_._1).map{ case (k,v) => k -> v.map(i => List(i._2, i._3))}.toList
groupedData: List[(String, List[List[Long]])] = List((C: - write,List(List(1412750402124, 433057), List(1412750402125, 433060))), (E: - read,List(List(1412750402124, 5435), List(1412750402125, 5445))), (F: - write,List(List(1412750402124

,1728),List(1412750402125,1428)),(F: - read,List(List(1412750402124,5435),List(1412750402125,5450))),(E: - write,List(List(List) 1412750402124,1728),列表(1412750402125,1730))),(C: - read,List(List(1412750402124,46552070),List( 1412750402125,46552071))))

请注意,结果为List[(String, List[List[Long]])]。这将使序列化更容易 Play有很棒的Json库,可以很容易地使用' Writes':

implicit val itemWrites = new Writes[(String, List[List[Long]])] {
  def writes(t: (String, List[List[Long]])) = Json.obj(
    "name" -> t._1,
    "data" -> t._2)
}

现在它可以序列化:

scala> Json.toJson(groupedData)
res23: play.api.libs.json.JsValue = [{"name":"C: - write","data":[[1412750402124,433057],[1412750402125,433060]]},{"name":"E: - read","data":[[1412750402124,5435],[1412750402125,5445]]},{"name":"F: - write","data":[[1412750402124,1728]

,[1412750402125,1428]]},{" name":" F: - read"," data":[[1412750402124,5435], [1412750402125,5450]]},{"名称":" E: - 写","数据":[[1412750402124,1728],[1412750402125,1730 ]],{"名称":" C: - 读","数据":[[1412750402124,46552070],[141275040212 5,46552071]]}]

相关问题