我可以在Java中编写包含连字符的变量名吗? [英] Can I write a variable name including hyphen in Java?

查看:377
本文介绍了我可以在Java中编写包含连字符的变量名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用GSON。我有以下JSON响应:

I am using GSON in my app. I have the following JSON response:

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

所以,我正在创建一个这样的类:

So, I am creating a class like this:

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

但是,我无法创建变量 person -name 。我该如何解决这个问题?

However, I am unable to create a variable person-name. How can I solve this?

推荐答案

选择一个有效的Java标识符并使​​用 @ SerializedName 注释告诉GSON相应JSON属性的名称:

Choose a valid Java identifier and use the @SerializedName annotation to tell GSON the name of the corresponding JSON property:

import com.google.gson.annotations.SerializedName;

class Person {
    boolean success;
    @SerializedName("person-name")
    String personName;
}

这篇关于我可以在Java中编写包含连字符的变量名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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