我可以在Java中GSON反序列化包含连字符的变量名称吗? [英] Can I GSON deserialize a variable name which includes a hyphen in Java?

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

问题描述

我在我的应用程序中使用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标识符,然后使用

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中GSON反序列化包含连字符的变量名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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