我正在学习斯卡拉。我试图找到一种将 JSON 字符串转换为 Scala 案例类实例的简单方法。Java 有一个很棒的库,叫做 Google Gson。它可以将 java bean 转换为 json 并返回而无需一些特殊的编码,基本上你可以在一行代码中完成。
public class Example{
private String firstField
private Integer secondIntField
//constructor
//getters/setters here
}
//Bean instance to Json string
String exampleAsJson = new Gson().toJson(new Example("hehe", 42))
//String to Bean instance
Example exampleFromJson = new Gson().fromJson(exampleAsJson, Example.class)
我正在阅读有关https://www.playframework.com/documentation/2.5.x/ScalaJson的信息,但无法理解:为什么 scala 如此复杂?为什么我要编写阅读器/编写器来序列化/反序列化简单的案例类实例?有没有简单的方法可以使用 play json api 转换案例类实例 -> json -> 案例类实例?