1
我有以下代码:如何将匿名类转换为json?
def test = Action {
val Anon = new {
val foo = "foo"
val bar = "bar"
}
Ok(Json.toJson(Anon))
}
而且我得到这个编译错误:
No Json deserializer found for type Object{val foo: String; val bar: String}. Try to implement an implicit Writes or Format for this type.
什么是快速解决这个问题?我已经在这里发现了与这个错误有关的另一个问题,但也许它更具体/更复杂。
没有反映,我不知道该怎么做... – 2013-03-01 14:17:30
@JulienLafont - 谁没有反射说什么?但我不想自己编写代码,而是使用一行代码。 – ripper234 2013-03-01 16:06:37
经过反思,我不认为有人已经这样做。尝试使用Anon.getClass.getDeclaredFields创建一个映射名称/值。 – 2013-03-01 19:50:52