I have a simple Scala class:
import com.google.gson.annotations.SerializedName
class MyTest(@SerializedName("my_test") var myTest: String)
extends Serializable { ... }
And I have this code:
val testJson = "{\"myTest\": \"my-test\"}"
val myTest = gson.fromJson(testJson, classOf[MyTest])
This works fine! The content of the variable of myTest is my-test.
In reality, my JSON looks like this:
val testJsonWithUnderscore = "{\"my_test\": \"my-test\"}"
val myTest = gson.fromJson(testJsonWithUnderscore, classOf[MyTest])
So, the variable name myTest does not match with the my_test of the JSON string. So I included @SerializedName("my_test") but it does not work. The var myTest is null.
The @SerializedName("my_test") does not work like expected. What is wrong here?