当case类只有一个字段时如何将json转换为case类 [英] How to turn json to case class when case class has only one field

查看:222
本文介绍了当case类只有一个字段时如何将json转换为case类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在游戏2.1中,读取操作用于将Json编组为对象.但是,当案例类只有一个字段时,我该怎么办呢?适用于更多字段的意识形态无效,因为不使用一个字段"and".因此,我没有获得FunctionBuilder.

In play 2.1 reads are used to marshall Json to objects. But how can I do this when the case class has only one field. The ideom that works for more fields does not work, as with one field 'and' is not used. Thus I do not get a FunctionBuilder.

以下代码为我提供了类型不匹配的信息.我该如何解决?

The following code gives me a type mismatch. How can I fix this?

case class Data(stamm: Seq[String])


implicit val dataReads  = (
  (__ \ "stamm").read(Reads.list[String])
)(Data)

推荐答案

Json组合器不适用于单个字段案例类.

Json combinators doesn't work for single field case class.

Pascal(此API的编写者)已在此处说明了这种情况 https://groups.google.com/forum/?fromgroups=#!starred/play-framework/hGrveOkbJ6U

Pascal (writer of this API) has explained this situation here https://groups.google.com/forum/?fromgroups=#!starred/play-framework/hGrveOkbJ6U

有些可行的解决方法,例如以下一种方法:

There are some workarounds which works, like this one:

case class A(value: List[Int])
val areads = (__ \ 'value).read[List[Int]].map{ l => A(l) } // covariant map

这篇关于当case类只有一个字段时如何将json转换为case类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆