Json 使用 gson 解析为 POJO 时总是 returns null

Json always returns null when parsing into POJO with gson

我有一个 Jackson 服务器将服务对象解析为 JSON,然后将其发送到 Android。在 Android 中,我有相同的服务 class 但 gson 总是 returns 一个空对象。

服务Class:

public class Service {
    public String ServiceName;
    public ArrayList<String> ParamList;
    public ArrayList<String> ParamType;

    public Service(String sn,ArrayList<String> pl,ArrayList<String> pt) {
        this.ServiceName = sn;
        this.ParamList = pl;
        this.ParamType = pt;
        // TODO Auto-generated constructor stub
    }

JSON 字符串:

{"ParamList":[],"ParamType":[],"ServiceName":"ServiceX"}

Android代码:

gson.fromJson(response,Client.Service.class)

Json 字符串来自日志,所以我知道服务器工作正常。依赖添加在Gradle:

的模块路径中
    implementation 'com.google.code.gson:gson:2.8.6'

这段测试代码没有任何问题:

public class Main {
    public static void main(String[] args) {
        Service service = new Gson().fromJson(
                "{\"ParamList\":[],\"ParamType\":[],\"ServiceName\":\"ServiceX\"}",
                Main.Service.class
                );
        
        System.out.println(service.ServiceName);
    }
    
    public static class Service {
        public String ServiceName;
        public List<String> ParamList;
        public List<String> ParamType;

        public Service(String sn, List<String> pl, List<String> pt) {
            this.ServiceName = sn;
            this.ParamList = pl;
            this.ParamType = pt;
        }
    }
}

所以,我认为你应该寻找 Gson 本身之外的任何其他原因。