Gson Unable to invoke no-args constructor for class

               

Although there are several threads on this topic.. please do not mark this as duplicate.

My pojo looks like this :

public class sample {    public sample() {        // TODO Auto-generated constructor stub    }    private String instructions;    private String resource;    private List<Map<String,String>> fields;    private String taskid;    private List<Map<String,String>> answer;    public String getTaskid() {        return taskid;    }    public void setTaskid(String taskid) {        this.taskid = taskid;    }    public String getInstructions() {        return instructions;    }    public void setInstructions(String instructions) {        this.instructions = instructions;    }    public String getResource() {        return resource;    }    public void setResource(String resource) {        this.resource = resource;    }    public List<Map<String,String>> getFields() {        return fields;    }    public void setFields(List<Map<String,String>> fields) {        this.fields = fields;    }    public List<Map<String,String>> getAnswer() {        return answer;    }    public void setAnswer(List<Map<String,String>> answer) {        this.answer = answer;    }}

I am doing a httpget and the result is an array of Json objects I try to typecast it to sample but it gives an exception.

the deserialization snippet is as follows

sample[] temp = gsonObj.fromJson(response, sample[].class);

the exception i get is

java.lang.RuntimeException: Unable to invoke no-args constructor for class [sample;. Register an InstanceCreator with Gson for this type may fix this problem.    at com.google.gson.MappedObjectConstructor.constructWithAllocators(MappedObjectConstructor.java:68)    at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:52)    at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:42)    at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:60)    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:104)    at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:76)    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:54)    at com.google.gson.Gson.fromJson(Gson.java:551)    at com.google.gson.Gson.fromJson(Gson.java:498)    at com.google.gson.Gson.fromJson(Gson.java:467)    at com.google.gson.Gson.fromJson(Gson.java:417)    at com.google.gson.Gson.fromJson(Gson.java:389)    at HTTPClientUtils.getResultsFromMobileWorks(HTTPClientUtils.java:327)

Can you please let me know where i am making the mistake ??

share improve this question
 
 
There is nothing wrong with the code you have posted, and your syntax is correct; it should work just fine. I highly suspect the code you are executing is not the code you have listed. Rebuild your project. –  Brian Roach  Oct 17 '12 at 4:47
 
you should edit and remove your company-name, and project name. Just for anonymity. –   Nishant  Oct 17 '12 at 4:51
 
Isn't Sample a nested class? Didn't the constructor throw an exception? –   maaartinus  Oct 17 '12 at 5:41

2 Answers

up vote 2 down vote accepted

Can't reproduce. But here what works:

public class Sample {    public Sample(){}    public int kk;    public List<Map<String,String>> fields;    public static void main(String[] args) {        String s = "[{\"kk\":1, \"fields\":[{\"a\":\"a1\"}]}, {\"kk\":5}, {\"kk\":2}, {\"kk\":8}, {\"kk\":6, \"fields\":[{\"b\":\"b1\"}]}]";        Sample[] r = new Gson().fromJson(s, Sample[].class);        for(Sample t: r)            System.out.println(">> " + t.kk + " " + t.fields);    }}

results:

>> 1 [{a=a1}]>> 5 null>> 2 null>> 8 null>> 6 [{b=b1}]

Sidenote:

  1. Capitalize your classes, always.
  2. post a sscce
           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43668118/article/details/86661121