java - GSON 库的 toJson() 返回错误的 json

标签 java android json gson

我正在使用 GSON 库将 json 作为 header 传递给服务器。 但它没有生成我预期的 json。

我的 Pojo 类“TestRequest.java”是这样的:

    public class TestRequest {

        private String mobileNumber;

        public TestRequest(String mobileNumber) {
            this.mobileNumber = mobileNumber;
        }
     }

这是我调用 GSON 类生成 json 的代码:

     Gson gson = new Gson();
     TestRequest tt = new TestRequest("+8801913000000");
     String json = gson.toJson(tt);

我期望的 json 是:

     {"mobileNumber":"+8801913000000"}

但我得到:

     {"aIf":"+8801913000000"}

注意:此代码在 2 天前运行良好。

最佳答案

尝试改变你的 pojo 类

public class TestRequest implements Serializable {

        @SerializedName("mobileNumber")
        private String mobileNumber;

        public TestRequest(String mobileNumber) {
            this.mobileNumber = mobileNumber;
        }
        public String getMobileNumber() {
            return mobileNumber;
        }

        public void setMobileNumber(String mobileNumber) {
            this.mobileNumber = mobileNumber;
        }
    } 

如果不行请告诉我

关于java - GSON 库的 toJson() 返回错误的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41563057/

相关文章:

mysql - 迭代 MySQL 记录并在 GoLang 中创建一个 JSON

java - 提交 Android 构建后出现 Flurry CN1Lib 错误

java - 在 Eclipse 中使用和测试 Web 服务

android - 移动计算中位置变化的节能检测

jquery - 如何判断 jquery tmpl 中是否存在子属性

javascript - 当 promise 失败时如何捕捉?

java - 编译 protobuf 以使用原始类而不是众所周知的类型

java - dropwizard 中的单元测试 View

android - 查询联系人的结构化名称返回 null

java - 如何连接蓝牙 socket 以在蓝牙打印机中打印数据