Java 当使用gson解析到POJO时,Json总是返回null

Java 当使用gson解析到POJO时,Json总是返回null,java,android,json,gson,Java,Android,Json,Gson,我有一个Jackson服务器,它将服务对象解析为JSON,然后发送给Android。在Android中,我有相同的服务类,但gson总是返回空对象 服务类别: public class Service { public String ServiceName; public ArrayList<String> ParamList; public ArrayList<String> ParamType; public Service(Stri

我有一个Jackson服务器,它将服务对象解析为JSON,然后发送给Android。在Android中,我有相同的服务类,但gson总是返回空对象

服务类别:

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
    }
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;
        }
    }
}
公共类主{
公共静态void main(字符串[]args){
Service=new Gson().fromJson(
“{\'ParamList\':[],\'ParamType\':[],\'ServiceName\':\'ServiceX\'”,
Main.Service.class
);
System.out.println(service.ServiceName);
}
公共静态类服务{
公共字符串ServiceName;
公开名单;
公共列表类型;
公共服务(字符串序号、列表pl、列表pt){
this.ServiceName=sn;
this.ParamList=pl;
this.ParamType=pt;
}
}
}

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

在将
响应从JSON
传递到
之前,您是否确保您的
响应不为空或null?这是Android Studio上的环境问题。重新构建了项目,并再次添加了依赖项,结果成功了
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;
        }
    }
}