Can I write variable names with hyphens in Java?

I use gson in my application I have the following JSON responses:

{
  "success":true,"person-name": "John"
}

So I'm creating a class like this:

class Person {
    boolean success;
    String person-name;
}

However, I cannot create variable person names How can I solve this problem?

Solution

Select a valid Java identifier and use the @ serializedname annotation to tell gson the name of the corresponding JSON attribute:

import com.google.gson.annotations.SerializedName;

class Person {
    boolean success;
    @SerializedName("person-name")
    String personName;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>